Xcode - [UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: 行 (7) 超出了第 (0) 部分的边界 (0)。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26034814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:41:48  来源:igfitidea点击:

Xcode - [UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (7) beyond bounds (0) for section (0).'

iosobjective-cxcodeuiscrollviewuicollectionview

提问by Declan Land

I've just implemented the AGPhotoBrowserclass into my Xcode project and I'm getting the error of:

我刚刚在AGPhotoBrowser我的 Xcode 项目中实现了这个类,我收到了以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (7) beyond bounds (0) for section (0).'

The code where the crash happens is here:

发生崩溃的代码在这里:

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.currentWindow.hidden && !_changingOrientation) {
        [self.overlayView resetOverlayView];

        CGPoint targetContentOffset = scrollView.contentOffset;

        UITableView *tv = (UITableView*)scrollView;
        NSIndexPath *indexPathOfTopRowAfterScrolling = [tv indexPathForRowAtPoint:targetContentOffset];

        [self setupPhotoForIndex:indexPathOfTopRowAfterScrolling.row];
    }
}

The crash seems to happen as soon as I exit the ViewControllerwhere this is implemented?

一旦我退出ViewController执行此操作的位置,崩溃似乎就会发生?

回答by Fawad Masud

The error tells you that your table does not have any row at index 7. It means that your table view consists of 7 rows and maximum range of indexPath.row can be 6 (because row index of a UITableView starts from 0). Whenever you will call a row at index path beyond range of the table, it will throw an error.

该错误告诉您,您的表在索引 7 处没有任何行。这意味着您的表视图由 7 行组成,并且 indexPath.row 的最大范围可以是 6(因为 UITableView 的行索引从 0 开始)。每当您在超出表范围的索引路径上调用一行时,它都会引发错误。

回答by AmitaiB

I received this error when I neglected to set the tableview's Delegate and DataSource; so it seems that nilis also "out of bounds".

当我忽略设置 tableview 的 Delegate 和 DataSource 时收到此错误;所以这似乎nil也是“越界”。

Xcode:6.4, iOS:8.x

Xcode:6.4, iOS:8.x

回答by saraman

Reload the TableViewdata before calling scrollToRowAtIndexPath().

TableView在调用之前重新加载数据scrollToRowAtIndexPath()

[contentTableView reloadData];