我开发了一个显示项目列表的简单 UITableView。在这个 UITableView 之上,我创建了一个 UIImageView 形式的选择栏,它移动到用户选择的任何行。我创建了两个按钮(一个向上,一个向下),它们也控制该选择栏的移动。当用户单击向上按钮时,选择栏恰好向上移动一排,当用户单击向下按钮时,选择栏向下移动一排。我的问题是,当我到达表格的最顶部时,如果用户单击向上按钮,我希望选择栏移动到表格的最底部,并且我希望选择栏移动到表格的最顶部如果用户单击向下按钮,则表。两个按钮调用相同的方法(我根据它们的标记值区分这两个按钮)。然而,
2013-06-06 11:34:03.124 SimpleTable[4982:c07] *** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630
2013-06-06 11:34:03.207 SimpleTable[4982:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x9489850> 2 indexes [0, 10])'
我不确定为什么会这样。这是我的向上和向下按钮调用的相关代码:
- (IBAction)buttonClicked:(id)sender {
if([sender tag] == 1){
if (_index.row == 0) {
_index = [NSIndexPath indexPathForRow:[_tableData count] inSection:_index.section];
}
else
_index = [NSIndexPath indexPathForRow:_index.row - 1 inSection:_index.section];
[UIView animateWithDuration:.3 animations:^{
CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];
CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
_imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}];
}
else if([sender tag] == 2){
if (_index.row == [_tableData count]) {
_index = [NSIndexPath indexPathForRow:0 inSection:_index.section];
}
else
_index = [NSIndexPath indexPathForRow:_index.row + 1 inSection:_index.section];
[UIView animateWithDuration:.3 animations:^{
CGRect rect = [self.view convertRect:[_table rectForRowAtIndexPath:_index] fromView:_table];
CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
_imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}];
}
}
谁能看到我做错了什么?
提前感谢所有回复的人。