简体   繁体   English

在Swift中未调用UICollectionView DidDeselectItemAtIndexPath方法

[英]UICollectionView DidDeselectItemAtIndexPath method not called in Swift

I've got a collection view listing a bunch of videos and tapping any of them will push navigation controller which contain a custom player view to play the video. 我有一个收藏视图,其中列出了一堆视频,然后轻按其中的任何一个,将推动包含自定义播放器视图的导航控制器来播放视频。 Tapping the close button on the customer player view will pop the current controller and go back to the video list controller. 点击客户播放器视图上的关闭按钮将弹出当前控制器,然后返回到视频列表控制器。

Also when tapping one of the cells that cell will become gray color. 同样,当点击其中一个单元格时,该单元格也会变成灰色。 When going back and tapping another cell from the video list, I want to deselect the previously selected cell and make it back to white and make the newly selected cell to be gray color. 返回并点击视频列表中的另一个单元格时,我想取消选择先前选择的单元格,然后使其恢复为白色,并使新选择的单元格为灰色。

The problem is, didDeselectCellAtIndexPath method is NEVER called. 问题是,从不调用didDeselectCellAtIndexPath方法。 The previously selected cell does get deselected, which I could see from the print of the selected indexPath. 先前选择的单元格确实被取消选择,我可以从所选indexPath的打印结果中看到。 However the delegation method never gets called thus backgroundColor never changes back to white. 但是,永远不会调用委托方法,因此backgroundColor永远不会变回白色。 It looks like multiple cells are selected, despite allowsMultipleSesection is already set to false. 尽管allowMultipleSesection已设置为false,但似乎已选择了多个单元格。

Following configuration is set: 设置以下配置:

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

Here is my collectionView methods and delegation methods: 这是我的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。

From the documentation I can understand that this method gets called when the user selected cell X, and then selects cell Y. Now cell X deselected and the method will be called. 从文档中,我可以理解,当用户选择单元格X,然后选择单元格Y时,将调用此方法。现在取消选择单元格X,将调用该方法。

Save an index of the selected cell before you move to the new view controller, and when you come back to the collection view controller, deselect the cell programmatically and then run inside your own function what you wanted to run in the deselect delegate method. 在移至新的视图控制器之前,请保存选定单元格的索引,然后当您返回到集合视图控制器时,以编程方式取消选择该单元格,然后在自己的函数中运行要在deselect委托方法中运行的函数。

The collection view calls this method when the user tries to deselect an item in the collection view. 当用户尝试取消选择集合视图中的项目时,集合视图将调用此方法。 It does not call this method when you programmatically deselect items. 以编程方式取消选择项目时,它不会调用此方法。 If you do not implement this method, the default return value is true. 如果不实现此方法,则默认返回值为true。

didDeselectItemAt is called when allowsMultipleSelection is set to true. didDeselectItemAt当被称为allowsMultipleSelection设置为true。

backgroundColor never changes back to white backgroundColor永远不会变回白色

even when your previously selected cell does get deselected, because your view doesnt get updated. 即使您先前选择的单元格确实被取消选择,因为您的视图不会更新。 You need to update your collection view cells view everytime you go back. 您每次返回时都需要更新集合视图单元格视图。 You can refresh complete UICollectionView in viewWillAppear of your collectionViewController subclass. 您可以在collectionViewController子类的viewWillAppear中刷新完整的UICollectionView。 You can also use @entire method to deselect all selected indexPath. 您也可以使用@entire方法取消选择所有选定的indexPath。

didSelectItemAt方法的末尾,在集合视图上调用deselectItem(at:animated:)方法。

Let the cell handle its background color. 让单元处理其背景色。 Just add the following to your "PreviewCell" class: 只需将以下内容添加到“ PreviewCell”类中:

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

If the parent Class doesn't implement a delegate method, any Subclass won't be able to do it either. 如果父类未实现委托方法,则任何子类也将无法执行该方法。

Please make sure the Class you are Subclassing implements it. 请确保您要子类化的类实现了它。

在您的didSelectItemAtIndexPath调用中:

for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) { [self.collectionView deselectItemAtIndexPath:indexPath animated:NO]; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView - 如果选择了单元格,则不会调用 didDeselectItemAtIndexPath - UICollectionView - didDeselectItemAtIndexPath not called if cell is selected 在编程创建的UICollectionView中未调用didDeselectItemAtIndexPath函数 - didDeselectItemAtIndexPath function is not being called in programmatically created UICollectionView Swift 2 - 调用`didDeselectItemAtIndexPath`时出现致命错误 - Swift 2 - Fatal Error when `didDeselectItemAtIndexPath` is called iPhone应用程序在UICollectionView的didDeselectItemAtIndexPath中崩溃 - iPhone application crashes in didDeselectItemAtIndexPath of UICollectionView UICollectionView中未调用数据源方法 - Datasource method is not called in UICollectionView 调用reloadData时didDeselectItemAtIndexPath不起作用 - didDeselectItemAtIndexPath is not working when reloadData is called UICollectionView在reloadData之后不调用didDeselectItemAtIndexPath - UICollectionView does not call didDeselectItemAtIndexPath after reloadData UICollectionView didDeselectItemAtIndexPath不获取单元格 - UICollectionView didDeselectItemAtIndexPath doesn't get cell UICollectionView didDeselectItemAtIndexPath 影响多个单元格并给出“nil” - UICollectionView didDeselectItemAtIndexPath effects multiple cells and gives "nil" UICollectionView cellForItemAtIndexPath方法没有被调用 - UICollectionView cellForItemAtIndexPath method is not getting called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM