13

使用我的应用程序使用 tableviews 时出现错误,错误看起来像这样。

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:]
第 (0) 行的第 (3) 行超出界限 (3)。

发生的情况是,当用户向下钻取我的导航视图时,他们可以从每个视图的表格视图中选择一个单元格,稍后将用于构建搜索字符串。

但是,如果他们犯了错误,我允许他们返回并更改他们的选择。当用户更改父视图的值然后进入子视图时,仅当先前的选择超出当前的条目数时才会发生此错误表视图..

通常这不会是一个大问题,但是,在 viewDidAppear 内部,我正在调用一个滚动到先前选择的单元格的方法......这显然是破坏应用程序并给我错误的原因。

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

我如何阻止它执行我已经尝试了大约 100 种不同的 if 父视图中的语句和子视图捕获新的 indexpath 并再次检查 oldselected indexpaths 然后设置

oldCheckedIndexPath = nil;

但是无论如何它总是会搞砸。

4

4 回答 4

28

干净的方式,假设self是 tableView 数据源:

检查表格视图中的部分数量:

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 之前,我通过另一个选项解决了它:此方法重新加载您的 tableview,[yourtableviewobject reloadData];然后调用:

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

我收到此错误是因为我试图滚动到空 tableView 的第 0 行第 0 部分。确保您的 tableData.count > 0。

于 2019-08-12T19:14:51.630 回答