我遇到了一个非常令人费解的错误。我的 UITableView 的第一行返回 1,第二行在 indexPath 中返回 0!这怎么可能呢?
在我的 `-(void)viewDidLoad` 中一切都很好。我成功地突出了第一行
currentRow = 0;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionNone];
我有变量 currentRow
用于跟踪选择了哪一行(另一个控件根据当前选择的行进行更改)。
现在在我的 `didDeselectRowAtIndexPath` 委托函数中,我有:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSLog(@"IndexPath: %@", [indexPath description]);
}
日志显示以下内容:
IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 0]
当我触摸 second 行时,IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 1]
当我触摸 first 行时。
没有行插入或删除或排序等,甚至没有滚动。它是一个简单的 UITableView,分组样式,有 1 节和 3 行。这可能是什么原因造成的?
谢谢你的帮助,S
您已实施 didDeselectRowAtIndexPath。当行不再被选中时,它将被触发。
当您触摸第二行时,第一行不再被选中,因此将显示 [0, 1]。当您再次触摸第一行时,第二行现在不再被选中,因此将显示 [0, 0]。
这些是完全可以预料的。
如果您需要在行被选中时做出响应,请实施 didSelectRowAtIndexPath。。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// ^
...
NSLog(@"IndexPath: %@", [indexPath description]);
}
尽管标题如此,重载的方法实际上是 didDeselectRowAtIndexPath
,所以听起来行为是正确的——当第 1 行被触摸时,第 0 行被取消选择,反之亦然。