Steve Steve - 7 months ago 148
Objective-C Question

How are cells deselected programmatically in UICollectionView when allowMultipleSelection is enabled?

I have allowMultipleSelection enabled in a collection view. The cells change to and from their
selected states when tapped. All good. However, when I want to reset the entire view to selected state:NO using the code below, the cells appears to be entirely deselected until I then make a new selection, at which point all previously selected cells show their previously selected state.

i.e. despite appearances the collectionview is not updating it's current selection list when I programmatically deselect the cells

- (void)clearCellSelections {
for (LetterCell *cell in self.collectionView.visibleCells) {
[cell prepareForReuse];

In custom cell:

- (void)prepareForReuse
[super prepareForReuse];
[self setSelected:NO];

What am I doing wrong? Is there another way to deselect all cells?

Thanks TBlue for taking a look


You can iterate over - [UICollectionView indexPathsForSelectedItems]:

for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];