我正在使用 PerformBatchUpdate 删除单个 collectionView 单元格,问题是如果我尝试删除第一个单元格(第 0 行),它会成功删除它,但如果我再次尝试删除第一个单元格(更新的单元格位于第 1 行,现在在第 0 行)它删除当前位于第 1 行的单元格,而不是删除第 0 行的单元格。
注意 - 请仅使用 PerformBatchUpdate 提出答案。
我正在使用此代码
private var movieViewModels = [movieViewModel]()
集合视图协议
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)
}
}
删除单元格的代码
func deleteMoviewhenFilterOFF(index:IndexPath){
self.movieViewModels.remove(at: index.row)
self.clctnView.performBatchUpdates {
self.clctnView.deleteItems(at: [index])
} completion: { success in
print("successfully updated")
}
}
注意:- 如果我使用以下代码在 BatchUpdate 中重新加载集合视图,它工作正常,但我认为 PerformBatchUpdate 会自动执行此操作,不建议在批量更新中重新加载集合视图
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()
}
}