繁体   English   中英

在Swift中未调用UICollectionView DidDeselectItemAtIndexPath方法

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

这是视频播放器视图控制器

弹出控制器后,列表控制器将显示两个选中的单元格,但仅选中了其中一​​个。即使当前只有一个选定的单元格,也不会调用DeselectionItemAtIndexPath。

从文档中,我可以理解,当用户选择单元格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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM