【问题标题】:Deselect invisible row取消选择不可见行
【发布时间】:2014-02-12 11:48:03
【问题描述】:

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

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)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)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tagTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

【问题讨论】:

    标签: ios uitableview selection


    【解决方案1】:

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

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

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

    【讨论】:

    • UIKit 中存在一个错误,当您无法取消选择不可见的选定行时。然后卡住了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2017-03-17
    • 2019-07-22
    • 2019-01-16
    相关资源
    最近更新 更多