简体   繁体   中英

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. The previously selected cell does get deselected, which I could see from the print of the selected indexPath. However the delegation method never gets called thus backgroundColor never changes back to white. It looks like multiple cells are selected, despite allowsMultipleSesection is already set to 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:

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.

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.

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.

didDeselectItemAt is called when allowsMultipleSelection is set to true.

backgroundColor never changes back to white

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. You can also use @entire method to deselect all selected indexPath.

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

Let the cell handle its background color. Just add the following to your "PreviewCell" class:

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]; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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