Previously selected UICollectionViewCells are deselected after non-selectable cell is tapped
ios, objective-c, uicollectionview, uicollectionviewcell, uikit
Solution
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeue...
cell.userInteractionEnabled = isSelectableIndexPath(indexPath)
return cell
}
func isSelectableIndexPath(indexPath: NSIndexPath) -> Bool {
//logic to check if cell is selectable
}
This works by disabling interaction with the cell.
Problem
I am implementing a UICollectionView with multiple selection enabled. Some of my cells are selectable, some are not. Here is the chain of events: - I select a few cells by tapping on them and returning `YES` to - `shouldHighlightItemAtIndexPath:` - `shouldSelectItemAtIndexPath:` - I try to select a non-selectable cell by tapping on it (the non-selectionable aspect is achieved by returning `NO` to `shouldSelectItemAtIndexPath:`) - Result: All selected cells are deselected and `didDeselectItemAtIndexPath:` is called on them. NOTE: `shouldDeselectItemAtIndexPath:` is not called. Expected Result: Nothing happens. Is this normal behavior? I can't find anything in the docs. If so, how can I go about not deselecting my cells?