6

I want to be able to select multiple rows like the default mail app shown below:

enter image description here

I have a button called edit that calls

[self.myTableView setEditing:YES animated:YES]

enter image description here

The edit button successfully shows the circles on the left of the cells like the mail app as shown above. However, when I actually select one of the rows, nothing happens. The red checkmark does not appear in the circle as I would expect. Why isn't the red checkmark appearing?

#pragma mark - UITableViewDataSource Methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
    }

    cell.textLabel.text = @"hey";

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

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

#pragma mark - Private Methods

- (IBAction)editButtonTapped:(id)sender {
    if (self.myTableView.editing) {
        [self.myTableView setEditing:NO animated:YES];
    }
    else {
        [self.myTableView setEditing:YES animated:YES];
    }
}
Adam Johns
  • 35,397
  • 25
  • 123
  • 176

1 Answers1

18

You have to explicitly set selection to be enabled during editing mode:

[self.tableView setAllowsSelectionDuringEditing:YES];

Or

[self.tableView setAllowsMultipleSelectionDuringEditing:YES];

According to the docs: these properties are set to NO by default.

If the value of this property is YES , users can select rows during editing. The default value is NO. If you want to restrict selection of cells regardless of mode, use allowsSelection.

Additionally, the following snippet of code may be causing problems with your selection because it immediately deselects the row as soon as it's been selected.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thank you! This was it. Also can be changed in IB on the table view (not table view controller) as "Editing: Single Selection During Editing" – jrc Oct 08 '15 at 02:34