Fry
Fry

Reputation: 6275

UICollectionView multiple selection doesn't deselect first cell

I've this issue with multiple selection in UICollectionView.

When I want select multiple cell simultaneously I use self.collectionView.allowsMultipleSelection = YES;

Suppose I tap on item at rows 0,1,2,3 (in this order), so when I remove the multiple selection setting self.collectionView.allowsMultipleSelection = NO; the item at row = 0 remains selected.

What I forgot? Is it a bug?

Upvotes: 2

Views: 5450

Answers (1)

Yuchen
Yuchen

Reputation: 33046

This is actually interesting. But nope, it is not a bug!

Fist of all, here are two different settings for UICollectionView that you need to pay attention to for your question:

allowsMultipleSelection

A Boolean value that determines whether users can select more than one item in the collection view.

allowsSelection

If the value of this property is YES (the default), users can select items.

By default, allowsMultipleSelection is set to NO and allowsSelection is set to YES. So you will be able to select cell but no more than one.

When you set allowsMultipleSelection to YES and select multiple cells and suddenly change the value back to NO, only one of the cells will remain selected. However, it doesn't necessary be the first one. In your case, any of the cells 0, 1, 2, 3 could be selected while the others are deselected. Apple doesn't seems to have a documentation of this behaviour. If you want to disable selection, setting allowsSelection to NO is a right way to do.

Upvotes: 4

Related Questions