我想要實現此功能:在我的應用程序,如果我選擇了一個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