Search code examples
iosobjective-cios6uicollectionview

How to get the index path of a UICollectionView header when using a tap gesture?


I get the touch point using locationInView and pass this to the collection view's indexPathForItemAtPoint. I will get an index path for a cell, but never a UICollectionReusableView (header/footer), as it always returns nil.


Solution

  • Probably too late to help on this but perhaps someone else will hit this as I did. The problem is there is no meaningful indexPath for the header (it appears to always return 0,0).

    Anyway, when I get the point, instead of the indexPath I'm checking to see whether it is inside the subview for the header:

    CGPoint point = [sender locationInView:collectionView];
    if (CGRectContainsPoint(CGRectMake(0.0f,0.0f,140.0f,140.0f), point))
       NSLog(@"Point was inside header");
    

    This works in my instance only because I know the size of the header and can safely assume its position within the collectionview because the collectionView has only one section (0).

    HTH