didSelectRowAtIndexPath返回错误的IndexPath [英] didSelectRowAtIndexPath returns wrong IndexPath
问题描述
我遇到了一个非常令人费解的错误。我的UITableView的第一个行返回 1 ,第二个在indexPath中返回 0 !怎么可能呢?
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?
在我的` - (void)viewDidLoad`中,一切都还可以。我正在成功突出显示第一行
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];
我有变量 currentRow
用于跟踪哪个行被选中(另一个控件根据当前选择的那个而改变)。
I have the variable currentRow
for tracking which row is selected (another control changes according to which one is currently selected).
现在在我的`didDeselectRowAtIndexPath`委托函数中我有:
Now in my `didDeselectRowAtIndexPath` delegate function I have:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSLog(@"IndexPath: %@", [indexPath description]);
}
日志显示以下内容:
IndexPath:< NSIndexPath 0x79509d0> 2个索引[0,0]
当我触摸第二个行时,
IndexPath:< NSIndexPath 0x79509d0>当我触摸第一行时,2个索引[0,1]
。
IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 0]
when I touch the second row, and
IndexPath: <NSIndexPath 0x79509d0> 2 indexes [0, 1]
when I touch the first row.
没有行插入或删除或排序等,甚至不滚动。它是一个简单的UITableView,分组样式,有1个部分和3行。造成这种情况的原因是什么?
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?
感谢您的帮助,
S
Thanks for your help,
S
推荐答案
您已实施 De selectRowAtIndexPath 。当不再选择行时,它将被触发。
You have implemented didDeselectRowAtIndexPath. It will be fired when the row is no longer selected.
当您触摸第二行时,第一行不再被选中,因此将显示[0,1]。
当您再次触摸第一行时,第二行现在不再被选中,因此将显示[0,0]。
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.
完全可以预期。
实施做了选择 RowAtIndexPath 如果你需要它来选择选择行。
Implement didSelectRowAtIndexPath if you need it to respond when the row is selected.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// ^
...
NSLog(@"IndexPath: %@", [indexPath description]);
}
这篇关于didSelectRowAtIndexPath返回错误的IndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!