didSelectRowAtIndexPath 返回错误的 IndexPath
我遇到了一个非常令人费解的错误。我的 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:
,并且 索引路径:
。
没有行插入、删除或排序等,甚至没有滚动。这是一个简单的 UITableView,分组样式,有 1 个部分和 3 行。可能是什么原因造成的?
感谢您的帮助,
S
I have encountered a really puzzling bug. The first row of my UITableView returns 1 and the second one returns 0 in the indexPath! How is that even possible?
In my `-(void)viewDidLoad` everything is still fine. I am highlighting the first row successfully with
currentRow = 0;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionNone];
I have the variable currentRow
for tracking which row is selected (another control changes according to which one is currently selected).
Now in my `didDeselectRowAtIndexPath` delegate function I have:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSLog(@"IndexPath: %@", [indexPath description]);
}
The log shows the following:
IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 0]
when I touch the second row, andIndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 1]
when I touch the first row.
There are is no row insertion or deletion or sorting, etc., not even scrolling. It is a simple UITableView, grouped style, with 1 section and 3 rows. What could be causing this?
Thanks for your help,
S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经实现了 没有取消选择RowAtIndexPath。当该行不再被选择时,它将被触发。
当您触摸第二行时,第一行不再被选中,因此将显示 [0, 1]。
当您再次触摸第一行时,第二行现在不再被选中,因此将显示 [0, 0]。
这些都是完全可以预料到的。
实施 didSelectRowAtIndexPath(如果您需要在选择行时响应)。选择。
You have implemented didDeselectRowAtIndexPath. It will be fired when the row is no longer selected.
When you touch the second row, the first row is no longer selected, so [0, 1] will be shown.
When you touch the first row again, the second row is now no longer selected, so [0, 0] will be shown.
These are totally expected.
Implement didSelectRowAtIndexPath if you need it to respond when the row is selected.
尽管有标题,重载方法实际上是
didDeselectRowAtIndexPath
,因此听起来行为是正确的 - 当触摸第 1 行时,第 0 行将被取消选择,反之亦然。Despite the title, the overloaded method is in fact
didDeselectRowAtIndexPath
, so it sounds like the behaviour is correct -- when the 1th row is touched, the 0th becomes deselected, and vice versa.