Jukurrpa
Jukurrpa

Reputation: 4168

Glitch in animation when using reloadSection in UITableView with static cells

I'm using a UITableView with static cells for the settings view of my application.

One of the sections of this table contains two cells, the second one only being visible when a UISwitch contained in the first one is turned on.

So the UISwitch's target is this:

- (void)notificationSwitchChanged:(id)sender
{
  UISwitch* switch = (UISwitch*)sender;

  self.bNotify = switch.on;
  [self.settingsTable reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}

I implemented numberOfRowsInSection: this way:

(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section != 1)
    return [super tableView:tableView numberOfRowsInSection:section];

  if (self.bNotify)
    return 2;
  return 1;
}

This "works", but the animation is faulty, making the first cell disappear or slide upwards halfway below the previous section or various stuff depending on the animation type I choose. The cleanest is UITableViewRowAnimationNone but is still not perfect.

If I scroll the section out of view and back it looks normal again.

EDIT: took a few screenshots of the problem:
Example Image

I tried adding beginUpdates and endUpdates before and after reloadSections but it didn't change anything. Using reloadData instead of reloadSections works but there's no animation at all.

Is it because the table's cells are static or am I missing something?

Upvotes: 1

Views: 1922

Answers (1)

Thomas Kekeisen
Thomas Kekeisen

Reputation: 4406

I think this method should solve your problem.

[self.tableView insertSections:<#(NSIndexSet *)#> withRowAnimation:<#(UITableViewRowAnimation)#>];

Just update the data source (you dont have to because your are updating the section count depending on some flags) and call it.

Maybe you have to use this method instead.

[self.tableView insertRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>];

Upvotes: 1

Related Questions