13

テーブルビューを使用するアプリケーションを使用するとエラーが発生します。エラーは次のようになります。

2012-01-19 10:19:51.442 bcode[1176:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (3) beyond bounds (3) for section (0).'
*** First throw call stack:
(0x16c3052 0x1920d0a 0x166ba78 0x166b9e9 0x68a136 0x3bfdf 0x6c2fbf 0x6c32d4 0x6c35d7 0x6d2666 0x87890e 0x878c17 0x878c86 0x62c499 0x62c584 0x2718e00 0x13614f0 0x15fa833 0x15f9db4 0x15f9ccb 0x1d05879 0x1d0593e 0x5fba9b 0x2838 0x2795 0x1)
terminate called throwing an exception(gdb) 

NSRangeException、-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]
行 (3) がセクション (0) の境界 (3) を超えています。

何が起こっているかというと、ユーザーがナビゲーション ビューをドリルダウンすると、各ビューのテーブルビューからセルを選択でき、後で検索文字列を作成するために使用されます。

ただし、エラーが発生した場合は、戻って選択を変更することを許可します..このエラーは、ユーザーが親ビューの値を変更してサブビューに移動すると、前の選択が現在のエントリ数を超えていた場合にのみ発生しますテーブルビュー..

通常、これは大きな問題にはなりませんが、viewDidAppear 内で、以前に選択したセルにスクロールするメソッドを呼び出しています...明らかにアプリが壊れてエラーが発生します。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //Scroll to previously selected value
    [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath  atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

これを実行しないようにするにはどうすればよいですか?親ビューとサブビューの if ステートメントが新しいインデックスパスをキャッチし、oldselected indexpaths を再度チェックしてから設定する場合、約 100 の異なる方法を試しました。

oldCheckedIndexPath = nil;

しかし、どういうわけか、とにかくいつもめちゃくちゃになります。

4

4 に答える 4

28

selftableViewデータソースであると仮定すると、クリーンな方法は次のとおりです。

テーブルビューのセクション数を確認します。

if ( [self numberOfSectionsInTableView:self.tableView]
                                     > oldCheckedIndexPath.section 

そのセクションの行数を確認します。

  && [self tableView:self.tableView numberOfRowsInSection:
          oldCheckedIndexPath.section] > oldCheckedIndexPath.row )
   {
      [self.tableView scrollToRowAtIndexPath: // etc etc etc

または、簡単なハック:

@try {
    [self.tableView scrollToRowAtIndexPath: // etc etc etc 
}
@catch ( NSException *e )
{
    NSLog(@"bummer: %@",e);
}
于 2012-01-18T21:46:28.433 に答える
5

受け入れられた答えに触発されました:

目的 C:

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

迅速:

let indexPath = IndexPath(row: rowToScroll, section: sectionToScroll) 
if (self.tableView.numberOfSections > indexPath.section && self.tableView.numberOfRows(inSection: indexPath.section) > indexPath.row ) {
    self.tableView.scrollToRow(at: IndexPath(row: rowToScroll, section: sectionToScroll), at: .top, animated: true) 
}
于 2017-06-28T22:39:56.360 に答える
1

scrollToRowAtIndexPath を呼び出す前に、別のオプションで解決しました。このメソッドは、テーブルビューをリロードして[yourtableviewobject reloadData];から呼び出します。

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([messages count] - 1) inSection:0];
[objTableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
于 2013-11-07T09:29:40.013 に答える