iOS:未选择单元格时的indexPathForItemAtPoint

时间:2013-10-15 16:17:25

标签: ios uicollectionview nsindexpath

在我的应用程序中,我想知道触摸的单元格的索引路径是什么,我使用它:

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

它工作正常,但问题是当我触摸另一个没有项目的空间时,索引的结果是0。 是否可以检查我是否触摸过物品?感谢

1 个答案:

答案 0 :(得分:4)

index为0,因为selectedIndexPath为零。你应该写

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

P.S。对于以前的(不正确的)答案,我很误解。