Question

In my app I want to know what's the indexpath of cell that I touch and I use this:

CGPoint location = [gesture locationInView:grid_element]; 
NSIndexPath *selectedIndexPath = [grid_element indexPathForItemAtPoint:location];
int index = selectedIndexPath.row;

It work fine but the problem is when I touch another space where there isn't a item, the result of index is ever 0. Is it possible to check if I don't touch inside an item? thanks

Était-ce utile?

La solution

index is 0 because selectedIndexPath is nil. You should be writing

CGPoint location = [gesture locationInView:grid_element]; 
NSIndexPath *selectedIndexPath = [grid_element indexPathForItemAtPoint:location];
if (selectedIndexPath != nil)
{
    NSInteger index = selectedIndexPath.row;
}

P.S. Sorry for previous (incorrect) answer, I misunderstood.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top