UITableView selection mode for each cell when editing

In mine UITableView, when it enters edit mode, I would like to select only a few selected cells. I know that a class UITableViewhas a property allowsSelectionDuringEditing, but this applies to everything UITableView. I do not see the appropriate delegate methods to set this for each cell.

The best solution I can come up with is install allowsSelectionDuringEditingin YES. Then, in didSelectRowAtIndexPath, filter out any unwanted choices if editing the table. Also, in cellForRowAtIndexPathchange these cells selectionStyleto None.

The problem is that switching to edit mode does not restart UITableViewCells, so they selectionStyledo not change until they scroll the screen. So, in setEditing, I also need to iterate over the visible cells and set them selectionStyle.

This works, but I'm just wondering if there is a better / more elegant solution to this problem. The main outline of my code is attached. Any suggestions are greatly appreciated! Thank.

- (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;
            }
        }
    }
}
+5
source share
1 answer

I'm not sure how your application works, but maybe you could try using the following in your DataSource definition:

// 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;

when you go into edit mode, use the function to filter the first level of selection, then to the second level of selection

0

All Articles