Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to didHighlightItemAtIndexPath without a call to didSelectItemAtIndexPath for UICollectionView

I have a UICollectionView where I overrode hitTest:withEvent: in my UICollectionViewCells in order to allow for taps just outside of the cells to register as taps on the cells.

When I do this and I tap just outside the cells that now register as hits, I get calls to didHighlightItemAtIndexPath and didUnhighlightItemAtIndexPath, but I don't get a call to didSelectItemAtIndexPath. If I tap inside the cell I get all of the expected highlight and select item calls as I did before.

I don't have any custom gesture recognizers set up and I don't override touchesBegan or anything like that.

So does anyone know under what conditions you get a call for didHighlightItemAtIndexPath without a call to didSelectItemAtIndexPath? Is there any way to get my didSelectItemAtIndexPath called? Thanks.

EDIT

I forgot to mention that my UICollectionView is within a Today Widget, so it is contained within the Notification Center scroll view. If I move my select code into the didUnhighlightItemAtIndexPath, then it is called when you tap outside the cell, but the result is that you can't actually scroll the Notification Center without selecting one of the cells.

So perhaps the difference between the highlighting and selecting that I'm experiencing here has something to do with the scroll view responder canceling the selection outside of the cell?

like image 338
Greg G Avatar asked Apr 16 '15 23:04

Greg G


1 Answers

OK, I figured out what was going on.

I added a new UITapGestureRecognizer to my UICollectionView. Implementing it like this led me the the solution:

- (void)cellSingleTap:(UITapGestureRecognizer *)sender
{
    CGPoint point = [sender locationInView:collectionView_];
    NSIndexPath *indexPath = [collectionView_ indexPathForItemAtPoint:point];
    [ .... ]
}

When I checked the points returned when I got highlighting but no selection, it became apparent that it happened when the point tapped on was within the section insets of the collection view layout. And when the taps were on the section insets, the indexPathForItemAtPoint calls returned nil.

So basically the collection view will highlight, but not select, taps that are outside cells but are within its section insets. As long as the taps are outside the cells and not within the insets, those taps will result in calls to didSelectItemAtIndexPath.

Since I would like taps within the insets to count as taps on cells, I was able to workaround this issue by adjusting the tap points before my call to indexPathForItemAtPoint.

like image 99
Greg G Avatar answered Nov 08 '22 11:11

Greg G