The first thing I do is to set the cell selected.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.selected = YES;
return cell;
}
And the cell is successfully selected. If the user touches the selected cell, than should the cell be deselected and the delegates be called. But this never happen.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
I know that the delegates are not called if I set the selection programmatically. The delegate and datasource are set.
However, this delegate gets called:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
return YES;
}
If I remove the cell.selected = YES
than everything is working. Is there any one who can me explain this behaviour?
The issue is, that the cell is selected, but the
UICollectionView
does not know anything about it. An extra call for theUICollectionView
solves the problem:Full code:
I keep the question to help someone who may face the same problem.
I think the solution @zeiteisen provided is a way around solution. The actual thing lies in the selection mode. There is nothing to be set the in property
cell.selected
.There are two properties of the
UICollectionView
nameallowsMultipleSelection
andallowsSelection
.allowsMultipleSelection
isNO
by default andallowsSelection
isYES
.If you want to select only one cell then the code @the initialization look like
Then the settings will allow you select only one cell at a time, not less not more. You must have to select at least one cell. The
didDeselectItemAtIndexPath
wont be called unless you select another cell. Tapping an already selectedUICollectionViewCell
won't make it to Deselect. This is the policy behind the implementation.If you want to select multiple cell then the code @the initialization should look like the following:
Then the settings will allow to select multiple cell. This settings allow none or multiple
UICollectionViewCell
to be selected. Number of cell selected will beThis is the policy behind the multiple cell selection.
If you are intended to use any of the above you are welcome to use apples api without extra headache, otherwise you got to find a way around to sneak into your goal using your own code or apple's api.
Reloading the cell was solution for me. What i need was changing image in cell for a brief moment. I assign image to imageView in cellForItemAtIndexPath and when user touches the cell i change image and run mycode then reload the cell.
Hope it helps someone.
Add this line to your didSelectItemAtIndexPath method
I had the same issue. I pre-selected cells upon table load yet I had to double tap a cell to deselect it. @zeiteisen answer helped me. However, I'm working in Swift 3 so I thought I'd give an answer in Swift.
In my case it was caused by using same logic for
as for
And when cell was selected shouldHighlightItemAt was returning false - and this prevented me to deselect that cell.
Hope this will save someone's time :)