以下の内容はhttps://xyk.hatenablog.com/entry/2014/10/02/154741より取得しました。


UITableView の`scrollToRowAtIndexPath:atScrollPosition:animated:`でエラー

環境:iOS Deployment Target 7.1

UITableView でスクロール位置を画面上部に戻そうとしてscrollToRowAtIndexPath:atScrollPosition:animated:メソッドを使っていたのだが、レコード0件時にエラーが発生した。

コード

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

エラー内容

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

レコードがあるときだけ実行するように修正

if ([self.tableView numberOfRowsInSection:0] > 0) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}



以上の内容はhttps://xyk.hatenablog.com/entry/2014/10/02/154741より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14