質問

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