I have multiple items in a CollectionView, but only a few of them should be selectable. I'm handling this with the delegate method:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
My problem occurs if a selectable item is selected and in the next step the selection of a not selectable item is rejected by shouldSelectItemAtIndexPath returning NO, the selected item gets deselected anyway.
I have also tried to use
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
But it's the same problem.
Is this the correct desired behaviour of UICollectionView? And if yes, how can i avoid the deselection of my last selected item if a not selectable item gets selected?
I have to correct my assumption: The last selection won't get deselected!
In order to change the appearance of the cell on selection I have overridden the setSelected accessor of UICollectionViewCell. When selecting a non selectable item, the accessor setSelected of the last selected cell gets called multiple times. First with the state NO, then with the state YES and in the end with NO again. The last state NO caused my cell to set its appearance to that of an non selected cell.
I don't know the reason for this weird behavior nor could i solve it.
My workaround is to change the appearance of selected cells directly in the ViewController.
Set the selected appearance in:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Remove the selected appearance:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
Check if the current cell is selected and change the appearance as intended.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
6 Years late, but maybe helpful to someone else. In cases like this, where certain cells should not be selectable in the first place, one can simply add isUserInteractionEnabled = false
to the custom UICollectionViewCell
's awakeFromNib()
function. This can also be done in the xib file but that's less explicit and might not be obvious to future maintainers.
I just had this same problem. I tried a variety of solutions and the first one that worked in my case was to reload and then reselect the selected cells. This worked for me whether I reloaded the whole collection view or just the cells that were getting stuck with a non-selected appearance.
Approaches that didn't work for me:
selected
property to true, or even toggling it false then true again.-selectItemAtIndexPath:animated:scrollPosition:
, or even deselecting it via -deselectItemAtIndexPath:
and then selecting it again.-reloadItemsAtIndexPaths:
-reloadData
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With