iosuicollectionviewuicollectionviewcelldeselect

deselecting cells that are currently not visible


I'm trying to deselect cells that were previously selected but are not currently visible in collection view. I'm using this code:

        let optionalSelectedItems = collectionView.indexPathsForSelectedItems()
        if let selectedItems = optionalSelectedItems{
        for indexPath in selectedItems{

         self.collectionView.deselectItemAtIndexPath(indexPath, animated: true)
         self.collectionView.delegate?.collectionView!(self.collectionView, didDeselectItemAtIndexPath: indexPath)
        }

There is some pretty important stuff going on in my implementation of func collectionView(_:didDeselectItemAtIndexPath:). Deselection triggers an event in some other view. This throws an error because I am deselecting cells that are currently off screen and hence not even in the memory. How do I put cells in the memory for just a brief period of time? I only need to deselect them so I'm guessing 0.1s would be more than enough.


Solution

  • You can store indexes of selected rows in separate array and select/deselect cells after creation in

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