gpt4 book ai didi

ios - UICollectionView DidDeselectItemAtIndexPath 方法未在 Swift 中调用

转载 作者:行者123 更新时间:2023-11-30 12:15:08 24 4
gpt4 key购买 nike

我有一个 Collection View ,其中列出了一堆视频,点击其中任何一个都会推送导航 Controller ,其中包含用于播放视频的自定义播放器 View 。点击客户播放器 View 上的关闭按钮将弹出当前 Controller 并返回到视频列表 Controller 。

此外,当点击其中一个单元格时,该单元格将变成灰色。当返回并点击视频列表中的另一个单元格时,我想取消选择之前选择的单元格并将其恢复为白色,并使新选择的单元格为灰色。

问题是,didDeselectCellAtIndexPath 方法永远不会被调用。先前选择的单元格确实被取消选择,我可以从所选索引路径的打印中看到这一点。然而,委托(delegate)方法永远不会被调用,因此背景颜色永远不会变回白色。看起来像是选择了多个单元格,尽管 allowedMultipleSesection 已设置为 false。

设置以下配置:

let layout = UICollectionViewFlowLayout()
collectionView?.collectionViewLayout = layout
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.allowsSelection = true
collectionView?.allowsMultipleSelection = false

这是我的 collectionView 方法和委托(delegate)方法:

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

This is the video player view controller

After the controller was popped, the list controller shows two cells selected, but only one of them is selected. The DeselectionItemAtIndexPath not called, even though there is ONLY ONE currently selected cell.

最佳答案

让单元格处理其背景颜色。只需将以下内容添加到您的“PreviewCell”类中:

override var isSelected: Bool {
didSet {
// TODO: replace .red & .blue with desired colors
backgroundColor = isSelected ? .red : .blue
}
}

关于ios - UICollectionView DidDeselectItemAtIndexPath 方法未在 Swift 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com