4

Does anyone know how to hide a number of cells from the grouped UITableView when entering in edit mode? I would like the rows to hide with animation effect as seen in the Contacts app when going out of editing mode.

As you know, when in Contacts editing mode, there are more rows than when switching back to normal mode. I would like to know how the switching is done smoothly.

Note that my UITableView subclass is loading static UITableViewCells from the same nib using IBOutlets.

mrd3650
  • 1,371
  • 3
  • 16
  • 24

2 Answers2

4

just an update for the ones who remove or insert more than one group of rows:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths: ......];
[self.tableView insertRowsAtIndexPaths: ......];
[self.tableView removeRowsAtIndexPaths: ......];
[self.tableView endUpdates];

:D

Enrico Bottani
  • 124
  • 1
  • 8
1

When you set the editing mode of the UITableView, you have to first update your data source and then insert/delete rows.

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

    // populate this array with the NSIndexPath's of the rows you want to add/remove
    NSMutableArray *indexPaths = [NSMutableArray new];

    if(editing) [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
    else [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];

    [indexPaths release];
}
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
  • Thanks @Michael. Some clarifications.. By '_update your data source_' you mean that I do an `if(myTableView.editing)` condition in the `tableView:cellForRowAtIndexPath:` delegate? Additionally I have to do the same condition in the `tableView:numberOfRowsInSection:` and `numberOfSectionsInTableView:` as well since I have less sections and rows in the editing mode. Is this right? – mrd3650 Sep 26 '11 at 06:46
  • Ok it solved it by preparing 2 different dictionary datasources (one for normal mode and another for editing mode) then in the `setEditing:animated:` call I simply switch the tv datasource between those dictionary datasources. Then with some other logic, I remove/insert the rows as you @Michael mentioned. – mrd3650 Sep 27 '11 at 07:01