Iphone ScrollToRowatineXpath出错

Iphone ScrollToRowatineXpath出错,iphone,ios,uitableview,nsindexpath,Iphone,Ios,Uitableview,Nsindexpath,我在使用使用TableView的应用程序时出错,错误如下所示 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

我在使用使用TableView的应用程序时出错,错误如下所示

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 ScrollToRowatineXpath:atScrollPosition:animated:

第(3)行超出第(0)节的边界(3)

现在的情况是,当用户深入查看我的导航视图时,他们可以从每个视图的tableview中选择一个单元格,稍后将使用该单元格构建搜索字符串

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

正常情况下,这不会是一个大问题,但是,在ViewDid中,我调用了一个滚动到以前选择的单元格的方法。。。这显然是破坏应用程序并给我错误的原因

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //Scroll to previously selected value
    [self.tableView scrollToRowAtIndexPath:oldCheckedIndexPath  atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
我在父视图和子视图中尝试了大约100个不同的if语句,捕捉新的indexpath并再次检查oldselected indexpath,然后设置

oldCheckedIndexPath = nil;

但是无论如何,它总是设法把事情搞砸。

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

检查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);
}

在调用ScrollToRowatineXpath之前,我通过另一个选项解决了这个问题:这个方法使用
[yourtableviewobject reloadData]重新加载tableview然后调用:

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([messages count] - 1) inSection:0];
[objTableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

受公认答案的启发:

目标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) 
}

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

哦,是的!!!真不敢相信我没想到那件事。。呜呜!!工作得很好!哈哈,在过去的5个小时里,我一直在绞尽脑汁试图破解这个!完美的多谢各位+幽默的NSLog为1!顺便说一句@C.Johns不要使用try。。。catch方法-这是一个非常糟糕的实践(至少在C-Java中是完全不同的事情…)。哈哈,是的,我笑了@jrtc27,是的,我使用了正确构造的if语句。。。它工作得很好。@C.Johns:很好。甚至比调用自己的委托方法更好的方法可能是查看实际数据<代码>[self.categories count]
更短,可能也同样有效。好吧,酷就可以了,等我弄清楚为什么在某些情况下不删除附件勾号后,很快就会玩一玩。。再次感谢您的帮助!:)