编辑时的UITableView每单元格选择模式

Bri*_*lva 5 iphone selection uitableview

在我UITableView进入编辑模式时,我只想选择几个单元格.我知道UITableView班级有财产allowsSelectionDuringEditing,但这适用于整体UITableView.我没有看到任何相关的委托方法来基于每个单元格进行设置.

我能提出的最佳解决方案是设置allowsSelectionDuringEditingYES.然后,didSelectRowAtIndexPath如果表视图正在编辑,则过滤掉任何不需要的选择.此外,在cellForRowAtIndexPath,将这些单元格更改selectionStyle为"无".

这个问题是进入编辑模式不会重新加载UITableViewCells,所以它们selectionStyle不会改变,直到它们滚动到屏幕外.所以,在setEditing中,我还必须迭代可见单元格并设置它们selectionStyle.

这有效,但我只是想知道这个问题是否有更好/更优雅的解决方案.附上我的代码的基本大纲.任何建议都非常感谢!谢谢.

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

    if (self.editing && ![self _isUtilityRow:indexPath]) return;
    // Otherwise, do the normal thing...
}

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    // UITableViewCell* cell = ...

    if (self.editing && ![self _isUtilityRow:indexPath])
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else
    {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    return cell;
}

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    if (editing)
    {
        for (UITableViewCell* cell in [self.tableView visibleCells])
        {
            if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
            {
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
        }
    }
    else
    {
        for (UITableViewCell* cell in [self.tableView visibleCells])
        {
            if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
            {
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tun*_*ing 0

我不确定您的应用程序是如何工作的,但也许您可以尝试在数据源定义中的某个位置使用以下内容:

// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)

进入编辑模式时,使用该功能过滤第一个选择级别,然后过滤到第二个选择级别