didSelectRowAtIndexPath 返回错误的 IndexPath

发布于 2024-10-01 05:08:48 字数 951 浏览 1 评论 0原文

我遇到了一个非常令人费解的错误。我的 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:当我触摸第二行时,有2个索引[0, 0],并且 索引路径:当我触摸第一行时,2个索引为[0, 1]

没有行插入、删除或排序等,甚至没有滚动。这是一个简单的 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, and
IndexPath: <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-10-08 05:08:48

您已经实现了 没有取消选择RowAtIndexPath。当该行不再被选择时,它将被触发。

当您触摸第二行时,第一行不再被选中,因此将显示 [0, 1]。
当您再次触摸第一行时,第二行现在不再被选中,因此将显示 [0, 0]。

这些都是完全可以预料到的。

实施 didSelectRowAtIndexPath(如果您需要在选择行时响应)。选择

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //                                       ^
    ...

    NSLog(@"IndexPath: %@", [indexPath description]);
}

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.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //                                       ^
    ...

    NSLog(@"IndexPath: %@", [indexPath description]);
}
小傻瓜 2024-10-08 05:08:48

尽管有标题,重载方法实际上是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文