PerformBatchUpdate method in collection view deleting the cell at wrong index

473 Views Asked by At

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()
            }
    
        }
0

There are 0 best solutions below