Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't select UITableViewCell when TableView setEditing is set

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];
    }
}
like image 391
Adam Johns Avatar asked Sep 03 '13 20:09

Adam Johns


1 Answers

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];
}
like image 62
Mick MacCallum Avatar answered Sep 29 '22 00:09

Mick MacCallum