每当我将足够多的项目加载到UITableView中时,我就可以向下滚动足够远的距离以将项目移出屏幕,我实现的didSelectRowAtIndexPath方法代码会吐出无效的索引值,这会使我的应用程序崩溃。
这是我的方法代码,相当标准:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath{
UITableViewCell *cell = [self.tableView.visibleCells objectAtIndex:indexPath.row];
selImg = cell.imageView.image;
[self next];
}
例如,如果tableView中有15行来自输入到数据源中的数据,则任何时候在屏幕上仅显示12行。根据tableView在0-11处的那些元素。如果我向下滚动到其他三行并选择一个,则tableView仍显示它具有元素0-11,但indexPath表示已选择了第14行。
这将引发以下NSRangeException:
'NSRangeException',原因:'***-[__ NSArrayI objectAtIndex:]:索引
14个超出范围[0 .. 11]'
在Google和SO上进行了一些搜索,但找不到与我类似的问题,我也不知道该如何解决此问题,因为这段代码或类似的代码似乎是获取所选索引值的标准方法行。
最佳答案
尝试修改您的代码
UITableViewCell *cell = [self.tableView.visibleCells objectAtIndex:indexPath.row];
至
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
关于ios - didSelectRowAtIndexPath返回界限索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38519145/