0

I have a UITableView with three sections, the second section has the table that shows insertion and deletion indicators in editing mode. I'm adding a cell for the insertion row in cellForRowAtIndexPath: when editing is YES. Also, when the table goes in to editing mode I reduce the number of sections so the third section doesn't show (it has a button in it that I want to hide when in editing mode). Unless I call [self.tableView reloadData] in setEditing I don't see the insertion row, but when I do call it there is no animation. What am I doing wrong?

- (void)setEditing:(BOOL)flag animated:(BOOL)animated

{
  [super setEditing:flag animated:YES];
  [self.tableView setEditing:flag animated:YES];
  //unless i add [self.tableView reloadData] i don't see the + row, but then there is no animation
  [self.tableView reloadData];

To determine number of sections I am doing this

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.editing ? 2 : 3;
}

To add the insertion row I am doing this in cellForRowAtIndexPath

 if (indexPath.row == [[[self recipe] tasks] count])
 {
    cell.textLabel.text = @"Add task...";
    cell.detailTextLabel.text = @"";

Any help greatly appreciated. I'm embarrassed to say how much time I've wasted on this!

ToddB
  • 2,490
  • 3
  • 23
  • 40
  • Additional info: (1) If I don't do the reloadData I don't see the insertion row AND also the 3rd section doesn't go away. As above, everything works great with reloadData but there ain't no animation – ToddB Aug 31 '12 at 02:48

2 Answers2

2

You need to use UITableView's update methods. Check out Apple's comprehensive guide on the subject for more details, but this code snippet should give you an idea. Note that you should do the opposite when your table view leaves editing mode.

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:SECTION_NEEDING_ONE_MORE_ROW];
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:SECTION_TO_DELETE];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
0

Many thanks, Carl. Perfect. I had read the Apple documentation several times but just didn't get it. A Google example sent me down the wrong path. Problem solved and it looks really nice. :)

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:1];
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:2];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic];
// update the datasource to reflect insertion and number of sections
// I added a 'row' to my datasource for "Add task..."
// which is removed during setEditing:NO
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

Todd

ToddB
  • 2,490
  • 3
  • 23
  • 40