UICollectionView how to deselect all

uitableview deselect all rows
uicollectionview selection
how to deselect selected uitableview cell
uicollectionviewcell
uicollectionview not selecting
didselect collectionview swift
uicollectionview edit mode
uicollectionview single selection swift

I have a FollowVC and FollowCell Setup with collection View. I can display all the datas correctly into my uIcollection view cell using the following code with no problem.

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

Note that I could 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 "Select" mode, I can perform the selction of each cell and a check mark will be displayed on the cell. However, what I want to do is to have a cancel buttom to disable all the selected cell and removing the checkImg.

I have 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
    }
}

The program crashes here giving me a fatal error: Unexpectedly found nil while unwrapping an optional error at

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

I dont know why it is having trouble unwrapping the cell to be my FollowCell which contains an instance of the checkImg. I already used it before in a similar situation in didSelectItemAtIndexPath and it seems to work?

Thanks,

Not all of the selected cells may be on screen at the point when you are clearing the selection status, so collectionView.cellForItemAtIndexPath(indexPath) may return nil. Since you have a force downcast you will get an exception in this case.

You need to modify your code to handle the potential nil condition but you can also make your code more efficient by using the indexPathsForSelectedItems property of 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
     }
 }

deselectAll:, Deselects all items in the collection view. (IBAction)deselectAll:(id)sender; the delegate's collectionView:shouldDeselectItemsAtIndexPaths: method to see if​  UICollectionView deselect all. Ask Question Asked 4 years, 8 months ago. Toggle select / deselect state of a UICollectionView Cell on tap - Swift. 0.

Using Extension in Swift 4

extension UICollectionView {

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

collectionView:shouldDeselectItemAtIndexPath , YES if the item should be deselected or NO if it should not. Discussion. The collection view calls this method when the user tries to deselect an item in the  Deselect all cells, scroll to the top and remove any existing share items from the array. If sharing is not enabled, set the share bar button to the default state and return. Make sure largePhotoIndexPath is set to nil. Call the convenience method you created above to update the share label. Update the bar button items accordingly. Adding a Share Button

To simplify further, you could just do

followCollectionView.allowsSelection = false
followCollectionView.allowsSelection = true

This will in fact correctly clear your followCollectionView.indexPathsForSelectedItems even though it feels very wrong.

How to deselect selected UITableView cell - Simple Swift , You have UITableView with customized cells style, selection color, etc. When you tap on the cell, slide to another VC and came back, your cell still selected. When your event-handling code determines that the transition is finished, it calls the finish Interactive Transition() or cancel Interactive Transition() method to remove the intermediate layout object and install the intended target layout object.

This answer may be useful in swift 4.2

 let selectedItems = followCollectionView.indexPathsForSelectedItems

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

How to deselect item in CollectionView ?, .microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/​selection , I SelectedItem property to null but it doesn't deselected. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Developer Forums Participation Agreement .

UIcollectionView select and deselect issue · Issue #78 · ReactorKit , a problem with collectionview select and deselct (cannot deselect after .bind(​to: reactor.action). all select, select does not work anymore  I am developing an application using Xamarin.iOS. For this I have used UICollectionView with CustomCell. All I want is how to handle a click of an item/cell and open a new Screen. I have used the following code in UICollectionViewSource class but nothing happens:

UICollectionView how to deselect all, I have a FollowVC and FollowCell Setup with collection View. I can display all the datas correctly into my uIcollection view cell using the following code with no 

Collection View Tutorial: Handling Single and Multiple Selection, This collection view tutorial shows you how to interact with collection cells and how to handle single and multiple Deselect all selected items.

Comments
  • Thanks. Exactly what I needed and works. I didnt need to use any reloadItems Code though. I did what you said and it works exactly like you said.
  • Or, if you like it more swifty: tableView.indexPathsForSelectedRows?.forEach({ tableView.deselectRow(at: $0, animated: false) })