Arrays 如何从集合视图中删除多个选定单元格?(swift)
我在集合视图中的所有单元格中都有5个数组 如何从集合视图中删除多个选定单元格Arrays 如何从集合视图中删除多个选定单元格?(swift),arrays,swift,uicollectionview,Arrays,Swift,Uicollectionview,我在集合视图中的所有单元格中都有5个数组 如何从集合视图中删除多个选定单元格 var _selectedCells : NSMutableArray = [] 删除按钮 @IBAction func DeleteButton(_ sender: UIBarButtonItem) { // How delete multiple selected cells from collection view } 添加单元格索引 func collectionView(_ collecti
var _selectedCells : NSMutableArray = []
删除按钮
@IBAction func DeleteButton(_ sender: UIBarButtonItem) {
// How delete multiple selected cells from collection view
}
添加单元格索引
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)")
}
}
删除单元格索引
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if self.EditToolBar.isHidden == true {
} else {
print("EditMode")
_selectedCells.remove(indexPath)
print("unselectedCells - \(_selectedCells)")
}
}
崩溃
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的项数无效。”。更新(5)后现有节中包含的项目数必须等于更新(5)前该节中包含的项目数,加上或减去从该节插入或删除的项目数(0插入,2删除),加上或减去移入或移出该节的项目数(0迁入,0迁出)。'
添加单元格索引
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)")
}
}
}
删除单元格索引
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)")
}
}
}
删除按钮操作
@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”)
}
}
您不能仅从集合的数据源中删除项,还必须告诉集合视图这些项已被删除。为此,请使用
deleteems(在IndexPath:[IndexPath])
。如该方法的文档中所述,您还可以从performBatchUpdates(…)调用它
如果您想同时进行多个更改,例如,如果您想同时添加项目和删除项目。在numberOfRowsInSection
方法中,返回\u selectedCells.count
然后在每个方法中删除和取消选择方法,relod.tableview对不起,我不需要他转到collectionview。我正在尝试更改self.collectionview.deleteItems(在:self.selectedCells)并在按下Delete按钮时应用程序崩溃。更新了我的答案
@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”)
}
}