環境: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]; }