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

标签 ios objective-c xcode uiscrollview uicollectionview

我刚刚在我的 Xcode 项目中实现了 AGPhotoBrowser 类,但出现以下错误:

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

发生崩溃的代码在这里:

#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];
    }
}

崩溃似乎是在我退出实现此功能的 ViewController 时立即发生的?

最佳答案

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

关于ios - Xcode - [UITableView _contentOffsetForScrollingToRowAtIndexPath :atScrollPosition:]: row (7) beyond bounds (0) for section (0).',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034814/

相关文章:

ios - 如何在 App Store 中创建类似特色页面的布局?

objective-c - 添加到数组后保留计数会增加吗?

ios - 将 Sprite Kit Physics Body 移动到用户触摸的位置而不通过

swift - 简单的 UITableView 源文件中的编辑器占位符错误

xcode - 命令 CompileSwiftSources 失败,退出代码非零

ios - managedObject 上的 setValuesForKeysWithDictionary 在 swift 中给出编译错误

objective-c - 距离计算误差是客观的 C

ios - MPMoviePlayerController 仅显示黑屏

ios - NSMutable 字典无法正常工作

ios - 如何在构建时自动重置 iPhone 模拟器