我遇到了一个非常令人费解的错误。我的 UITableView 的 first 行返回 1 和 second 一个返回 indexPath 中的 0 !这怎么可能?
在我的 `-(void)viewDidLoad` 中一切都还好。我正在使用
成功突出显示第一行currentRow = 0;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionNone];
我有变量 currentRow
用于跟踪选择了哪一行(另一个控件会根据当前选择的行进行更改)。
现在在我的 `didDeselectRowAtIndexPath` 委托(delegate)函数中,我有:
-(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]);
}
关于iphone - didSelectRowAtIndexPath 返回错误的 IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118993/