点击一行将选择多行

发布于 2025-01-02 08:54:38 字数 780 浏览 2 评论 0原文

我的桌面视图中有一个奇怪的问题。我想选择行并且只想显示复选标记。我有一个包含多个部分的表格视图。选择该行并显示复选标记没问题,但是在表格视图中向下 10 行,该行也会被选中???当选择多于一行时,再往下 10 行,会出现多个选择。

我正在使用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        // Reflect selection in data model
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        // Reflect deselection in data model
    }
}

I have a weird issue in my tableview. I want to select rows and only want to show the checkmark. I have a tableview with multiple sections. Selecting the row and displaying the checkmark is fine, but 10 rows down the tableview, that row will also get selected??? When selecting more than one row, again, 10 lines down, multiple selections appear.

I am using:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        // Reflect selection in data model
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        // Reflect deselection in data model
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沉鱼一梦 2025-01-09 08:54:38

这不是选择另一行,而是重复使用同一复选标记的单元格。在 tableView:didSelectRowAtIndexPath: 中,您使用单元格来存储状态。在 tableView:cellForRowAtIndexPath: 中,您没有解决该行是否应该有复选标记的问题。

您可能想要做的是拥有一个 ivar NSMutableSet 并在 didSelectRow.. 中添加或删除 indexPath 从集合中,取决于其检查状态。然后在 cellForRow.. 中根据 indexPath 在集合中的成员身份设置 accessoryType 属性。

编辑抱歉,我没有注意到您在数据模型中保存状态的注释。在这种情况下,您所要做的就是在 cellForRow... 中添加检查。由于这是一个常见问题,我将保留原始答案。

That's not another row being selected, that's the same check-marked cell being reused. In tableView:didSelectRowAtIndexPath: you are using the cell to store state. In tableView:cellForRowAtIndexPath: you are not addressing whether the row should have a check mark or not.

What you will likely want to do is have an ivar NSMutableSet and in didSelectRow.. either add or remove the indexPath from the set, depending on it's checked status. Then in cellForRow.. set the accessoryType property based on the indexPath's membership in the set.

Edit Sorry I didn't catch the comments where you save the state to in a data model. In that case all you have to do is add the check in cellForRow.... Since this is a common question I'll leave the original answer up.

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