我有自定义CollectionView
单元格和按钮点击我正在调用closure
它是在下面实现的cellForItem
下面是代码
cell.closeImageTappped = { [weak self] cell in
guard let strongSelf = self else {
return
}
if let objectFirst = strongSelf.selectedFiles.first(where: {$0.fileCategory == cell.currentSelectedCellType && $0.fileName == cell.currentObject?.fileName}) {
cell.imgPicture.image = nil
cell.imgPlusPlaceHolder.isHidden = false
objectFirst.removeImageFromDocumentDirectory()
strongSelf.selectedFiles.remove(at: strongSelf.selectedFiles.index(where: {$0.fileName == objectFirst.fileName})!)
strongSelf.arraySource[indexPath.section].rows.remove(at: indexPath.row)
strongSelf.collectionViewSelectFile.performBatchUpdates({
strongSelf.collectionViewSelectFile.deleteItems(at: [indexPath])
}, completion: nil)
}
}
在某些情况下应用程序崩溃,例如我按得太快关闭多个单元格
在这里崩溃
strongSelf.arraySource[indexPath.section].rows.remove(at: indexPath.row)
致命错误:索引超出范围
当我检查行时
▿ 11 个元素 - 0 : 0 - 1 : 1 - 2 : 2 - 3 : 3 - 4 : 4 - 5 : 5 - 6 : 6 - 7 : 7 - 8 : 8 - 9 : 8 - 10 : 8
虽然 IndexPath 是
po indexPath
▿ 2 elements
- 0 : 0
- 1 : 11
如果我得到这样的 indexPath,它会显示正确的 IndexPath
self?.collectionViewSelectFile.indexPath(for: cell)
▿ Optional<IndexPath>
▿ some : 2 elements
- 0 : 0
- 1 : 9
但是为什么IndexPath
不一样呢self?.collectionViewSelectFile.indexPath(for: cell)