0

I have this code implemented in my tableView:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return NO;
    }
    return YES;
}

It does what I want, but I want to go one step better and make "section 0" disappear altogether when the edit button is pressed (this effect can be seen if you go into the "keyboard" menu on iOS and select edit in the top right corner, the top two sections disappear in an animation). I have attempted to temporarily remove the first section, but my app crashes when [tableView reloadData]; is called:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tvController.editing == YES) {
        return 1;
    }else if (tvController.editing == NO) {
        return 2;
    }
    return 0;
}

Also, I do not think I will end up with an animation if I get that code working, I think my approach is wrong. Thanks for your help guys!

prince
  • 506
  • 6
  • 18
  • Have you investigated the use of `deleteSections::` and `insertSections::` in `UITableView`? – Phillip Mills Jul 04 '12 at 13:39
  • So if I use deleteSections to delete section 0 would section 1 then become section zero? – prince Jul 04 '12 at 13:44
  • Yes section 1 will become section 0. But just an observation. Looking at the way that keyboard thingy works on the iPad, I would almost say they are two completely different views and they are animating the transition from one to the other. Note how the navigation button on top left disappears. Having said that you could do it as Phillip suggest via deleteSections and some animation. – Cliff Ribaudo Jul 04 '12 at 14:00

1 Answers1

1

Your problem

One of your sections is longer than the preceding one.

Since you hide section 0 by reporting 1 less section in numberOfSectionsInTableView:, in editing mode every delegate method must adjust the section number. One of them is not doing so.

// every delegate method with a section or indexPath must adjust it when editing

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tvController.editing) section++;
    return [[customers objectAtIndex:section] count];
}

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
    int section = indexPath.section;
    if (tvController.editing) section++;

    id customer = [[customers objectAtIndex:section] indexPath.row];

    // etc
}

My approach

UITableView reloadSections:withRowAnimation: reloads specified sections with an animation. Call it from your setEding:animated: delegate method.

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

    UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animation];

    [self.tableView reloadSectionIndexTitles];

    self.navigationItem.hidesBackButton = editing;
}

Your delegate also needs indicate that the hidden section has no rows or title.

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.editing && section == 0) {
        return 0;
    }

    return [[customers objectAtIndex:section] count];
}

- (NSString*) tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger) section
{
    if (self.editing && section == 0) {
        return nil;
    }

    [[customers objectAtIndex:section] title];
}
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
  • Hey, thanks for your answer, but could you explain the question mark here: UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone; and also, why, when I use UITableViewRowAnimationFade does it not fade? – prince Jul 04 '12 at 15:39
  • That is the ternary operator, http://stackoverflow.com/questions/10592547/what-does-this-do – Lachlan Roche Jul 04 '12 at 15:52
  • Thanks! that makes sense, but I still don't get animation when animated is set to YES. – prince Jul 04 '12 at 15:56
  • inserting and deleting rows versus sections has no effect. – prince Jul 04 '12 at 16:26