0

I am using PerformBatchUpdate for deleting a single collectionView Cell, The problem is if I try to delete the first cell (row 0) it successfully deletes that but if I again try to delete the first cell (updated cell which was at row 1, now at row 0) it deletes the cell which is currently at row 1 instead of deleting the cell at row 0.

Note - please suggest the answer using PerformBatchUpdate ONLY.

Am using this code

private var movieViewModels = [movieViewModel]()

Collection view protocols

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if isFiltering{
            return filteredMovies.count
        }
        return movieViewModels.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if isFiltering{
           return customiseCell(Arr: filteredMovies, index: indexPath, collectionView: collectionView)
        }else{
            return customiseCell(Arr: movieViewModels, index: indexPath, collectionView: collectionView)
        }
    }

Code for deleting cell

func deleteMoviewhenFilterOFF(index:IndexPath){

        self.movieViewModels.remove(at: index.row)
        self.clctnView.performBatchUpdates {
            self.clctnView.deleteItems(at: [index])
        } completion: { success in
            print("successfully updated")
        }

    }

NOTE :- It works fine if I reload the collection view in BatchUpdate using the below code but I think PerformBatchUpdate does this automatically its not recommended to reload the collection view in batch update

  func deleteMoviewhenFilterOFF(index:IndexPath){
    
            self.movieViewModels.remove(at: index.row)
            self.clctnView.performBatchUpdates {
                self.clctnView.deleteItems(at: [index])
            } completion: { success in
                print("successfully updated")
                self.clctnView.reloadData()
            }
    
        }
DevilAyan
  • 31
  • 5
  • Unclear what the problem is. In one place you say "crash", in another place you say the wrong thing is deleted. Which is it? – matt Dec 04 '21 at 18:14
  • Hey @matt both are happening, let me explain if the correct index is not deleted then when i reach at the last cell and tap delete. Then I got the crash (index out of range). I believe if we can delete the correct cell the crash will fix automatically. – DevilAyan Dec 04 '21 at 18:19
  • @matt Changed the title of the question to make more sense – DevilAyan Dec 04 '21 at 18:21
  • It seems to me that a lot depends upon the index path you pass when you call `deleteMoviewhenFilterOFF`. But you have not shown any code that calls it. – matt Dec 04 '21 at 19:27

0 Answers0