scrollToRowAtIndexPath出错

C.J*_*hns 11 iphone uitableview nsindexpath ios

我在使用我的使用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) 
Run Code Online (Sandbox Code Playgroud)

NSRangeException,-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]
第(3)行超出了第(0)节的边界(3).

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

但是我允许他们返回并更改他们的选择如果他们发生了错误..当用户更改父视图的值然后进入子视图时,只有当前一个选择超出当前条目数时才会发生此错误实现代码如下..

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

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

如何阻止执行我在父视图和子视图中尝试了大约100个不同的if语句捕获新的索引路径并再次检查旧选择的索引路径然后设置

oldCheckedIndexPath = nil;
Run Code Online (Sandbox Code Playgroud)

然而无论如何它始终设法搞砸了.

mvd*_*vds 26

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

检查tableview中的部分数量:

if ( [self numberOfSectionsInTableView:self.tableView]
                                     > oldCheckedIndexPath.section 
Run Code Online (Sandbox Code Playgroud)

并检查该部分中的行数:

  && [self tableView:self.tableView numberOfRowsInSection:
          oldCheckedIndexPath.section] > oldCheckedIndexPath.row )
   {
      [self.tableView scrollToRowAtIndexPath: // etc etc etc
Run Code Online (Sandbox Code Playgroud)

或者,快速黑客:

@try {
    [self.tableView scrollToRowAtIndexPath: // etc etc etc 
}
@catch ( NSException *e )
{
    NSLog(@"bummer: %@",e);
}
Run Code Online (Sandbox Code Playgroud)

  • +1对于幽默的NSLog!BTW @ C.Johns不使用try ... catch方法 - 这是非常糟糕的做法(至少在C语言中 - Java完全是另一回事......). (3认同)

pka*_*amb 6

受到已接受答案的启发:

Objective-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];
}
Run Code Online (Sandbox Code Playgroud)

迅速:

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) 
}
Run Code Online (Sandbox Code Playgroud)