2017-07-17 84 views
1

我正在使用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 
    } 
} 
+0

我想,當你在第二電池接頭所以首先得到deselected.You首先再次輕敲並保持其選擇didselect方法被調用,並有你檢查它去別的?您需要在某處保存選定的索引狀態。 –

回答

3

試試這個:

該解決方案對於多重選擇

1-作出取消選擇如下

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) 
    } 

2 - 註釋/刪除取消選擇方法在此方法中

func toggleSelection(moment : MomentModel) 
{ 
    moment.isSelected = !moment.isSelected 
    self.layer.borderWidth = moment.isSelected ? 3 : 1 
    self.layer.borderColor = moment.isSelected ? Constant.APP_BLUE_COLOR.cgColor : UIColor.red.cgColor 

} 
+0

答案中沒有有效的更改。您剛剛通過使用三元運算符代替其他代碼來優化代碼。 – Nish

+0

我已經提到刪除你的didDeselectItemAt方法。 – KKRocks

+0

好的,謝謝。無論如何,它wouldnt工作,因爲我設置selction false cellForItem中的每個單元通過引用[答案]上的其他答案(https://stackoverflow.com/questions/20380512/uicollectionview-diddeselectitematindexpath-not-called-if-cell-is - 選中)我悲傷地混合了各種答案。 – Nish

2

我想

3變化當您點擊第二個單元格時,會調用didselect方法。可能是你忘了:

_collectionView.allowsMultipleSelection = YES 
1

後很長一段時間我troubleshooted的問題,這是爲我工作...

我是做cellForItemAtIndexPath以下的事情:這是錯誤的

cell.isSelected = false
collectionView.selectItem(at:indexPath,animated:false,scrollPosition:.left)

我這樣做,因爲didSelect交替調用,而不考慮細胞的選擇。我只是取消了這段代碼,它爲我工作。現在didSelect正在調用第一次點擊,如果我再次單擊相同的單元格,那麼只按預期的流程調用didDeselect。

還要確保allowsMultipleSelection是真實的,也是allowsSelection是真的

相關問題