![](/img/trans.png)
[英]UICollectionView - didDeselectItemAtIndexPath not called if cell is selected
[英]UICollectionView DidDeselectItemAtIndexPath method not called in Swift
我有一個收藏視圖,其中列出了一堆視頻,然后輕按其中的任何一個,將推動包含自定義播放器視圖的導航控制器來播放視頻。 點擊客戶播放器視圖上的關閉按鈕將彈出當前控制器,然后返回到視頻列表控制器。
同樣,當點擊其中一個單元格時,該單元格也會變成灰色。 返回並點擊視頻列表中的另一個單元格時,我想取消選擇先前選擇的單元格,然后使其恢復為白色,並使新選擇的單元格為灰色。
問題是,從不調用didDeselectCellAtIndexPath方法。 先前選擇的單元格確實被取消選擇,我可以從所選indexPath的打印結果中看到。 但是,永遠不會調用委托方法,因此backgroundColor永遠不會變回白色。 盡管allowMultipleSesection已設置為false,但似乎已選擇了多個單元格。
設置以下配置:
let layout = UICollectionViewFlowLayout()
collectionView?.collectionViewLayout = layout
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.allowsSelection = true
collectionView?.allowsMultipleSelection = false
這是我的collectionView方法和委托方法:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! PreviewCell
cell.snapShotImageView.image = videoInfoArray[indexPath.item].previewImg
cell.durationLabel.text = videoInfoArray[indexPath.item].lengthText()
cell.dateLabel.text = videoInfoArray[indexPath.item].dateAddedText()
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! PreviewCell
cell.backgroundColor = UIColor.rgb(red: 240, green: 240, blue: 240)
let url = URL(fileURLWithPath: videoInfoArray[indexPath.item].path)
let vc = VideoController()
self.videoController = vc
vc.url = url
self.navigationController?.pushViewController(vc, animated: true)
}
override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! PreviewCell
cell.backgroundColor = UIColor.white
cell.captionFileLabel.backgroundColor = .white
print("Deselect called!!! This line should be printed, but it never happens!!!!")
}
從文檔中,我可以理解,當用戶選擇單元格X,然后選擇單元格Y時,將調用此方法。現在取消選擇單元格X,將調用該方法。
在移至新的視圖控制器之前,請保存選定單元格的索引,然后當您返回到集合視圖控制器時,以編程方式取消選擇該單元格,然后在自己的函數中運行要在deselect委托方法中運行的函數。
當用戶嘗試取消選擇集合視圖中的項目時,集合視圖將調用此方法。 以編程方式取消選擇項目時,它不會調用此方法。 如果不實現此方法,則默認返回值為true。
didDeselectItemAt
當被稱為allowsMultipleSelection
設置為true。
backgroundColor永遠不會變回白色
即使您先前選擇的單元格確實被取消選擇,因為您的視圖不會更新。 您每次返回時都需要更新集合視圖單元格視圖。 您可以在collectionViewController子類的viewWillAppear中刷新完整的UICollectionView。 您也可以使用@entire方法取消選擇所有選定的indexPath。
在didSelectItemAt
方法的末尾,在集合視圖上調用deselectItem(at:animated:)
方法。
讓單元處理其背景色。 只需將以下內容添加到“ PreviewCell”類中:
override var isSelected: Bool {
didSet {
// TODO: replace .red & .blue with desired colors
backgroundColor = isSelected ? .red : .blue
}
}
如果父類未實現委托方法,則任何子類也將無法執行該方法。
請確保您要子類化的類實現了它。
在您的didSelectItemAtIndexPath
調用中:
for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) { [self.collectionView deselectItemAtIndexPath:indexPath animated:NO]; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.