6

I have a problem with the UITableView Selection. If I click on the edit button the TableView goes into edit mode and loses the current selection.

How can I go into the edit mode without losing the selection?

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;

}
jrturton
  • 118,105
  • 32
  • 252
  • 268

2 Answers2

3

I was able to reproduce it: if you select the row like this [self.tableView selectRowAtIndexPath:] the cell loses its selection. But if you select the cell directly, it doesn't lose the selection on edit, something like this:

    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:
                          [NSIndexPath indexPathForRow:1 inSection:0]];

    [cell setSelected:YES animated:NO];
AlexR
  • 5,514
  • 9
  • 75
  • 130
  • 3
    When you select the cell directly, the table doesn't know that something is selected. That can cause other problems. In edit mode no cell is selected because it makes no sense. – Sulthan Dec 12 '11 at 10:22
1

Override setEditing:animated: in your table view controller. If you are going into edit mode, store the current selection as an NSIndexPath instance variable:

selectedRow = [self.tableView indexPathForSelectedRow];

If you are coming out of edit mode, set the table's selection to the stored index path:

[self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];

Be careful not to try and select a row that was deleted whilst editing took place. And remember to call the super implementation as well!

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • this solves the problem when i click the edit button, but when i swipe for delete a row it also loose the current row selection. –  Dec 11 '11 at 15:21
  • For that you'd do a similar thing in the canEditRow method in your question. – jrturton Dec 11 '11 at 15:24
  • But that wouldn't be normal behaviour - the swipe to delete is basically selecting that row, isn't it? – jrturton Dec 11 '11 at 15:33
  • i don't no why is there a property or something like that i missed? –  Dec 11 '11 at 15:37
  • my code looks like that inline - (IBAction)setTableEditing { [self setEditing:!self.editing animated:YES]; } - (void)setEditing:(BOOL)editing animated:(BOOL)animated { NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated]; [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; } –  Dec 11 '11 at 15:40
  • I mean, when you are swiping to delete a table row, that should really remove the selection - you've now selected the row that you are deleting, so it doesn't make sense to reselect a different row after the delete is done. – jrturton Dec 11 '11 at 16:57
  • hi jrturton, when i swipe to delete a row, the row loose the selection. if i decide to don't delete the row and click again on the row the delete botton goes and there is no selection on the table. –  Dec 12 '11 at 08:53
  • That s expected behaviour. If you did want to use the previous selection, there's no table view delegate method to tell when a cell has had deletion cancelled, you'd need to look at a custom cell subclass and setEditing: in there. But your original question, which I think I have answered, was about putting the whole table in editing mode. – jrturton Dec 12 '11 at 09:20