2014-10-17 95 views
0
UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView indexPathForRowAtPoint:point]]; 

这存在的崩溃是我的应用程序崩溃,就行了。是否有任何提示或建议如何防止发生崩溃。如果索引超出数组,可能根本不会执行这段代码。如何防止 - [__ NSArrayM objectAtIndex:]:指数1超出范围[0 .. 0]在这种情况下

+0

拆分代码为两行。检查'indexPathForRowAtPoint'的返回值。 – rmaddy 2014-10-17 14:20:55

+0

我在想如果这个观点不可见,会发生什么? – gabbler 2014-10-17 14:25:19

回答

0

你不能保证indexPathForRowAtPoint:会返回一个有效的索引路径,所以在使用前进行验证:

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point]; 
if (indexPath) { 
    UITableViewCell* cell=[_tableView cellForRowAtIndexPath:indexPath]; 
} 
相关问题