【发布时间】:2017-08-04 23:55:18
【问题描述】:
我有一个收藏视图,其中列出了一堆视频,点击其中任何一个都会推送包含自定义播放器视图的导航控制器来播放视频。点击客户播放器视图上的关闭按钮将弹出当前控制器并返回视频列表控制器。
此外,当点击其中一个单元格时,该单元格将变为灰色。当返回并从视频列表中点击另一个单元格时,我想取消选择之前选择的单元格并将其恢复为白色并将新选择的单元格变为灰色。
问题是,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!!!!")
}
【问题讨论】:
-
为什么你的委托方法有关键字
override?我的意思是,你在继承什么类?如果父类没有实现委托方法,那么任何子类也无法实现。 -
@zero UICollectionViewController、UICollectionViewDelegateFlowLayout、UIImagePickerControllerDelegate、UINavigationControllerDelegate,我正在继承或采用这些类或协议。当我删除“覆盖”时,它会弹出错误提示我添加“覆盖”。
-
在
collectionView配置下试试这个:clearsSelectionOnViewWillAppear = false -
@zero Dude 请在下面写一个答案,复制您所做的第一条评论“如果父类没有实现委托方法,任何子类也不会”,我会接受它作为正确答案。是你的暗示救了我的命。是的,我没有采用 UICollectionViewDelegate,它是唯一可以完成这项工作的。谢谢。
标签: ios swift uicollectionview