Kevin Sylvestre
Kevin Sylvestre

Reputation: 38012

Multi Select Table View Cell and no Selection Style

I have a basic UITableView that I want to enable the Mail.app style check marks while having no selection style. I have the following snippet:

#define UITableViewCellEditingStyleMultiSelect (3)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleMultiSelect;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

However, this will never display the check marks on selection (although the empty circles are displayed). Any ideas on how to fix it? I know it uses undocumented features but I'd really like to add support for the check marks. My actual example uses a very customized UITableViewCell and I can't enable the selection style!

sample table view

Upvotes: 3

Views: 6605

Answers (1)

Felix
Felix

Reputation: 35384

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

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    if (self.tableView.isEditing) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleMultiSelect;
}

-(IBAction) switchEditing {
    [self.tableView setEditing:![self.tableView isEditing]];
    [self.tableView reloadData]; // force reload to reset selection style
}

Upvotes: 6

Related Questions