UICollectionSee how to deselect all

I have a FollowVC and FollowCell program with a collector view. I can correctly display all the data in the uIcollection view cell using the following code without problems.

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FollowCell", forIndexPath: indexPath) as? FollowCell {

        let post = posts[indexPath.row]

        cell.configureCell(post, img: img)

        if cell.selected == true {
            cell.checkImg.hidden = false
        } else {
            cell.checkImg.hidden = true
        }
        return cell
    }
}

Please note that I can also select and deselect multiple images using the following code

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if deletePressed == true {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
        cell.checkImg.hidden = false
    } else {
        let post = posts[indexPath.row]
        performSegueWithIdentifier(SEGUE_FOLLOW_TO_COMMENTVC, sender: post)
    }
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
    cell.checkImg.hidden = true
}

When in the "Select" mode I can select each cell, and a checkmark will be displayed in the cell. However, what I want to do is cancel buttom to disable the entire selected cell and remove checkImg.

I tried

    func clearSelection() {
    print("ClearSelection posts.count = \(posts.count)")

    for item in 0...posts.count - 1 {
        let indexP = NSIndexPath(forItem: item, inSection: 0)
        followCollectionView.deselectItemAtIndexPath(indexP, animated: true)
        let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
        cell.checkImg.hidden = true
    }
}

A program crash here gives me a fatal error: Suddenly found zero, deploying an additional error in

let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell

, , FollowCell, checkImg. didSelectItemAtIndexPath , , ?

,

+12
3

, , collectionView.cellForItemAtIndexPath(indexPath) . , .

, nil, indexPathsForSelectedItems UICollectionView

 let selectedItems = followCollectionView.indexPathsForSelectedItems
 for (indexPath in selectedItems) {
     followCollectionView.deselectItemAtIndexPath(indexPath, animated:true)
     if let cell = followCollectionView.cellForItemAtIndexPath(indexPath) as? FollowCell {
        cell.checkImg.hidden = true
     }
 }
+23

Swift 4

extension UICollectionView {

    func deselectAllItems(animated: Bool) {
        guard let selectedItems = indexPathsForSelectedItems else { return }
        for indexPath in selectedItems { deselectItem(at: indexPath, animated: animated) }
    }
}
+6

To simplify things further, you can simply do

followCollectionView.allowsSelection = false
followCollectionView.allowsSelection = true

This actually clears your followCollectionView.indexPathsForSelectedItems correctly even if it looks very wrong.

0
source

Source: https://habr.com/ru/post/1648211/


All Articles