Alpinista
Alpinista

Reputation: 3741

Odd behavior with UICollectionViewDelegate

I have a viewController which contains a UICollectionView in which I have implemented both delegate methods shouldSelectItemAtIndexPath and didSelectItemAtIndexPath. The shouldSelect method is working as expected, and is getting called for each cell that is selected.

The didSelect method does not get called on the first cell that is tapped, even if tapped multiple times. But if you tap a second cell the didSelect method is called, but the indexPath.item value is for the previously tapped cell.

For example, I tap the first cell in the collectionView, and shouldSelect is called, with it's indexPath.item value = 0. DidSelect is not called, even for multiple taps on the cell.

If I then tap on any other cell, say for example, the 4th cell in the collectionView, shouldSelect is called and its indexPath.item value = 3. didSelect is also called, but it's indexPath.item value = 0 (the previously selected cell).

If I continue to select different cells, didSelect continues to be called, but the indexPath.item value is always for the previously selected item. If I tap a cell multiple times, didSelect fails to be called until I tap a different cell.

Anybody? I'm stumped. all other datasource and delegate methods seem to be working fine. Delegate and datasource are wired up correctly in the storyboard.

Upvotes: 3

Views: 887

Answers (1)

Paulius Dragunas
Paulius Dragunas

Reputation: 1712

Make sure that your

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

is not actually

- (void)collectionView:(UICollectionView *)collectionView didDeSelectItemAtIndexPath:(NSIndexPath *)indexPath

(DidDeSelect vs. DidSelect)

I had that same issue.

Upvotes: 9

Related Questions