我的智慧到此为止,但是我的collectionview出现了一个奇怪的问题,并试图选择多个项目。我有一个编辑按钮,当我按下它时会触发一个isEditing函数。轻触单元格时,它会更改isSelected上的布尔值。 我想做的是选择多个单元格,然后按下垃圾桶按钮以删除所选项目。我遇到两个问题:
我的编辑代码在这里:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
collectionView.allowsMultipleSelection = editing
let indexPaths = collectionView.indexPathsForVisibleItems
for indexPath in indexPaths {
let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
cell.isInEditingMode = editing
}
}
和我的删除按钮代码在这里:
if let selectedCells = collectionView.indexPathsForSelectedItems {
for indexPath in selectedCells {
if let items = dataSource.itemIdentifier(for: indexPath) {
var snapshot = dataSource.snapshot()
container.viewContext.delete(items)
saveChangesToDisk()
snapshot.deleteItems([items])
dataSource.apply(snapshot, animatingDifferences: true)
}
}
isEditing = false
}
我的完整项目在这里(专门链接到我的collectionview.swift:https://github.com/analogpotato/superdiff/blob/master/superdiff/CollectionViewController.swift