我正在使用didSelectItemAt和didDeselectItemAt來選擇多個collectionViewCell。我想選擇單元格,如果選擇了邊框爲藍色,並且取消選中「selected」單元格並將邊框設置爲默認值。但我的問題是,didDeselectItemAt交替調用。當我點擊任何一個單元格然後didSelectItemAt被調用,如果我點擊任何其他單元然後didDeselectItemAt被調用。我猜這不應該發生。 didDeselectItemAt應僅在我正在點擊已選單元格時調用。如果我出錯了,請糾正我。我已經refered這個UICollectionView - didDeselectItemAtIndexPath not called if cell is selected1但力爲我工作:(didSelectItemAt和didDeselectItemAt在swift 3.0中沒有像預期的那樣工作
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let cell = collectionView.cellForItem(at: indexPath) as! MomentDetailCell
let moment = self.arrOfMoments[indexPath.row] as! MomentModel
cell.toggleSelection(moment: moment)
self.arrOfDeletingImgs.append(moment)
}
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
let cell : MomentDetailCell = self.collectionViewImages.cellForItem(at: indexPath) as! MomentDetailCell
let moment = self.arrOfMoments[indexPath.row] as! MomentModel
cell.toggleSelection(moment: moment)
self.arrOfDeletingImgs.remove(at: (find(objecToFind: moment))!)
}
//而且這是我使用的類的代碼。我也讓allowsMultipleSelection真正在viewDidLoad中
extension MomentDetailViewController : UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 75, height: 75)
}
}
//這是我的customCell代碼
func toggleSelection(moment : MomentModel)
{
if (isSelected)
{
moment.isSelected = true
self.layer.borderWidth = 3
self.layer.borderColor = Constant.APP_BLUE_COLOR.cgColor
}
else
{
moment.isSelected = false
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.red.cgColor
}
}
我想,當你在第二電池接頭所以首先得到deselected.You首先再次輕敲並保持其選擇didselect方法被調用,並有你檢查它去別的?您需要在某處保存選定的索引狀態。 –