【发布时间】:2018-11-25 18:32:48
【问题描述】:
我的 UICollectionView 有一个奇怪的问题。当我在集合视图中选择第一个单元格时,最后一个单元格会突出显示,但不会显示在所选索引路径的数组中。为什么会这样?
这是我用来选择和取消选择单元格的代码?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = highlightedCellBorderWidth
cell?.layer.borderColor = UIColor.yellow.cgColor
selectedImages.append(imageArray[indexPath.item])
print(selectedImages)
print(collectionView.indexPathsForSelectedItems)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 0
let index = selectedImages.index(of: imageArray[indexPath.item])
selectedImages.remove(at: index!)
print(selectedImages)
print(collectionView.indexPathsForSelectedItems)
}
【问题讨论】:
-
集合视图单元格被重复使用。
标签: ios uicollectionview swift4 uicollectionviewcell