Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add extra cells in a UITableView in editing mode?

Tags:

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;
}
like image 442
Zoran Simic Avatar asked Sep 21 '09 10:09

Zoran Simic


1 Answers

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.

like image 85
Joost Avatar answered Oct 02 '22 11:10

Joost