views:

769

answers:

1

Do you know how to have some cells appear in a table view after the table goes into editing mode? Just like the "Contacts" iPhone app does when you edit a contact.

Maybe I'm wrong, but when editing the contacts, it looks like a grouped UITableView is used.

I tried this:

[self.tableView setEditing:YES animated:YES];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];

My table has only 1 section when not editing it, I wanted to add an extra section when editing (to keep it simple), but the call to 'insertSections' above crashes (all my table delegates take into account the 1 or 2 sections well depending on self.editing, I tried showing both sections in normal mode, and it works fine)

For 'numberOfSectionsInTableView' I have:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.editing) return 1;
    return 2;
}
+1  A: 

Where did you enter the editing state of the tableView? You should do it in -[UINavigationController setEditing:animated:]:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];

    if(editing){
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        // delete section
    }
}

So instead of calling setEditing on the tableView, call it on the navigationController (probably self) The controller will then handle the editing mode.

So the controller has an editing mode, and the tableView has. The tableView will be in editing mode once you set it programmatically, or when the user swipes to delete a row. In the latter case, the controller is not in editing mode, but the tableView is.

JoostK
Excellent! Indeed, the problem was a confusion with what was in editing mode. Thanks much!
Zoran Simic