Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView's allowsMultipleSelectionDuringEditing - don't display edit circle for some cells

I am trying to delete some rows in my UITableView by setting allowsMultipleSelectionDuringEditing to YES. This is all working well; the circle is showing on the left hand side.

However, for certain cells, I don't want the circle on the left hand side to come up. How do I do that? I've tried cell.selectionStyle = UITableViewCellSelectionStyleNone during editing and that didn't work.

Any hints?

like image 989
Herman Avatar asked Jul 08 '12 20:07

Herman


2 Answers

In order to disallow some rows from multiple selection you should use tableView:shouldIndentWhileEditingRowAtIndexPath: mixed with cell.selectionStyle = UITableViewCellSelectionStyleNone.

Here is an example from my code:

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath*)indexPath {
    if (indexPath.row < 4) {
        return YES;
    } else {
        return NO;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // (...) configure cell

    if (indexPath.row < 4) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}
like image 180
lyzkov Avatar answered Oct 14 '22 04:10

lyzkov


First, use these settings:

self.tableView.allowsMultipleSelectionDuringEditing = true
self.tableView.setEditing(true, animated: false)

And implement next delegate methods:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return self.shouldAllowSelectionAt(indexPath)
}

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return self.shouldAllowSelectionAt(indexPath)
}

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if self.shouldAllowSelectionAt(indexPath) {
        return indexPath
    }
    return nil
}

shouldAllowSelectionAt is my private method which contains logic about which row to select

like image 43
Nadzeya Avatar answered Oct 14 '22 04:10

Nadzeya