OGeek|极客世界-中国程序员成长平台

标题: ios - 取消选择不可见行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:08
标题: ios - 取消选择不可见行

我在更新 UITableView 中的行时遇到问题。在我的 tableView 部分中只能选择一行(可以有几个部分)。因此,当用户选择行时,我以编程方式取消选择本节中的其他行。当取消选择的行可见时,一切正常。如果任何取消选择的行不可见,则仍会对其进行检查。但是 didDeselectRowAtIndexPath 是为此行调用的。为什么?如何解决这个问题?

- (NSIndexPath*)tableViewUITableView *)tableView willSelectRowAtIndexPathNSIndexPath*)indexPath
{
    // It is predefined group - only one row in section cab be selected
    // Deselect other rows in section
    for (NSIndexPath * selectedIndexPath in tagTableView.indexPathsForSelectedRows) {
        if (selectedIndexPath.section == indexPath.section) {
            [self tableView:tableView didDeselectRowAtIndexPath:selectedIndexPath];
            [tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
        }
    }

    return indexPath;
}

- (void)tableViewUITableView *)tableView didDeselectRowAtIndexPathNSIndexPath *)indexPath
{
    [tagTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}



Best Answer-推荐答案


单元格的选定状态不应保存在 UITableViewCell 中,而应保存在填充单元格的对象(模型)中。

由于表格单元被重用,因此状态表示填充它们的模型的最后一个状态。您应该将所选状态保留在填充单元格的对象中,保留选择 indexPaths 的数组。

-tableView:cellForRowAtIndexPath: 中的设置该单元格的正确状态。如果您的模型对象保持选定的状态,您可以根据该属性设置状态,或者如果您使用选择索引路径保留一个数组,请检查路径是否在数组中并相应地设置状态。

关于ios - 取消选择不可见行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21282603/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4