views:

195

answers:

0

In my app, I have a UITableView, grouped style. The table has two modes. In mode 1, it shows one section (A), and in mode 2, it shows 2 sections (A and B). I transition between the two modes via the insertSection and deleteSection methods of UITableView. In addition, I have header text for each section

This works great for the most part, with section B animating smoothly in and out. However, I have found one strange corner case. When section B has zero rows in it, when I go from mode 2 to 1, the header text does not disappear. Instead, it stays behind, interfering with the rest of the table.

I have replicated this behavior in a generic, UITableView app. If anyone could help, it would be greatly appreciated!

Here is some code from the generic app I made to repro the bug:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  if (deleted)
    return 1;
  else
    return 2;
}
//"deleted" is a simple BOOL flag and is set to YES when a button is pressed:
-(IBAction)deleteSection:(id)sender
{
  if (!deleted) {
    deleted = YES;
    [(UITableView *)self.view deleteSections:[NSIndexSet indexSetWithIndex:1]
                            withRowAnimation:UITableViewRowAnimationRight];
  }
}

Code to create the rows:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (section == 0)
    return 7;
  else
    return 0;  // <-- if you increase this, everything works
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
  ...Initialize stuff...

  cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row+1];

  return cell;
}

Header creation:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  if (section == 0)
    return @"Section 1"; // i.e. array element
  else
  return @"Section 2";
}

Nothing fancy, but I can't figure it out. Thanks in advance!