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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top