Vlad Karlugin
Vlad Karlugin

Reputation: 71

How delete multiple selected cells from collection view? (swift)

I have 5 array in all my cells in collection view.

How can I delete multiple selected cells from collection view?

var _selectedCells : NSMutableArray = []

Delete button

@IBAction func DeleteButton(_ sender: UIBarButtonItem) {
       // How delete multiple selected cells from collection view
}

Add cell index

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     if self.EditToolBar.isHidden == true {
          self.performSegue(withIdentifier: "DetailVC", sender: indexPath.item)
     } else {
          print("EditMode")
          _selectedCells.add(indexPath)
          print("selectedCells - \(_selectedCells)")
     }
}

Delete cell index

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
     if self.EditToolBar.isHidden == true {
     } else {
          print("EditMode")
          _selectedCells.remove(indexPath)
          print("unselectedCells - \(_selectedCells)")
     }
}

Crash

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (5) must be equal to the number of items contained in that section before the update (5), plus or minus the number of items inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

Upvotes: 1

Views: 3413

Answers (2)

Tushar Katyal
Tushar Katyal

Reputation: 422

var _selectedCells = [IndexPath]()

Add Cell Index

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     if self.EditToolBar.isHidden == true {
          self.performSegue(withIdentifier: "DetailVC", sender: indexPath.item)
     } else {
          print("EditMode")
         if !(_selectedCells.contains(indexPath)) {
              _selectedCells.add(indexPath)
              print("selectedCells - \(_selectedCells)")
         }

     }
}

Delete cell Index

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
     if self.EditToolBar.isHidden == true {
     } else {
          print("EditMode")

        if let index = _selectedCells.index(where: { $0 == indexPath }) {
       _selectedCells.remove(at: index)
        print("unselectedCells - \(_selectedCells)")

         }   
     }
}

Delete Button Action

@IBAction func DeleteButton(_ sender: UIBarButtonItem) {
       // Two Things To make sure of 
       // 1. Never call reloadData() right after insert/move/deleteRows..., the insert/move/delete operation reorders the table and does the animation
       // 2. Call insert/move/deleteRows... always after changing the data source array.


       // remove the data from data Source Array you passed ,of selected cells you're trying to delete .

     self.collectionView.performBatchUpdates({
         self.collectionView.deleteItems(at indexPaths: _selectedCells)
     }){
              // optional closure
            print(“finished deleting cell”)
      }



}

Upvotes: 1

Caleb
Caleb

Reputation: 124997

You can't just delete items from the collection's data source, you have to also tell the collection view that those items have been removed. To do that, use deleteItems(at indexPaths: [IndexPath]). As described in the docs for that method, you can also call it from performBatchUpdates(...) if you have several changes that you want to make all at once, e.g. if you want to both add items and remove items at the same time.

Upvotes: 0

Related Questions