我想要实现此功能:在我的应用程序,如果我选择了一个UICollectionView单元格,然后边框变成蓝色,而如果我选择另外一个,以前的应该取消选择,边界应该变得透明。还有就是我写的方法:didDeselectItemAt indexPath不触发
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell
/* Set some settings */
if globalSelected[indexPath.item] {
cell.circleView.layer.borderColor = UIColor.blue.cgColor
} else {
cell.circleView.layer.borderColor = UIColor.clear.cgColor
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Global variable for maintain selection
global.selectedChatPath = indexPath
globalSelected[indexPath.item] = true
collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if indexPath != nilPath {
globalSelected[indexPath.item] = false
collectionView.reloadData()
}
}
的nilPath只是IndexPath(项目:-1,部分:0),但不要紧,因为collectionView(_ collectionView:UICollectionView,didDeselectItemAt indexPath:IndexPath)甚至没有被调用。我的CollectionView有allowSelection =真和allowsMultipleSelection =假性能。我会感谢任何帮助。
附加委托的CollectionView “collectionView.delegate =自我” – Ragul
@Ragul谢谢!但它已经完成了:我有另一个类,它符合UICollectionViewDataSource和UICollectionViewDelegate协议,并且我将这个类的一个实例声明为我的UICollectionView的委托和数据源。另外,第一步工作正常:点击任何单元格后,边框变成蓝色(这意味着代表团可以正常工作),但是当我点击其他单元格时,它们的边框也会变成蓝色,但以前的边框细胞仍然是蓝色的(我想透明)。 –
只是重新加载collectionView每个选择 – Ragul