4

I have a UITableView with two sections (a top section and a bottom section). When items are "checked off" in the top section (section 0), I'm moving them them to the bottom section (section 1) and vice versa. Everything is working great save for the animation.

I'm using the following, but the row movement is sluggish - I've seen better results in other apps. I'd like the rows from the top section to animate cleanly to the bottom section ... and the rows from the bottom section to animate cleanly to the top section when they are checked or unchecked.

// set the guests arrival status and use animation
    [guestList beginUpdates];
    if (!guest.didArrive) {
        [guest setDidArrive:YES];
        [guestList reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        [guest setDidArrive:NO];
        [guestList reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationTop];
    }
    [guestList endUpdates];

[guestList reloadData];

How should I code this for nice smooth animation?

Edit:

I found the problem. The could should be written this way instead:

//NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];

    // set the guests arrival status and use animation
    [guestList beginUpdates];
    if (!guest.didArrive) {
        [guest setDidArrive:YES];
        [guestList reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        [guest setDidArrive:NO];
        [guestList reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
    }
    [guestList endUpdates];
[guestList reloadData];

Note the first line that I commented out. I neglected to add this originally. As you can see, I was using a poorly constructed NSIndexSet.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
mySilmaril
  • 207
  • 1
  • 3
  • 13

2 Answers2

6

Problem solved. Please see edited response. Code should be:

// set the guests arrival status and use animation
[guestList beginUpdates];
if (!guest.didArrive) {
    [guest setDidArrive:YES];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
} else {
    [guest setDidArrive:NO];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}
[guestList endUpdates];
[guestList reloadData];
mySilmaril
  • 207
  • 1
  • 3
  • 13
  • This is the correct answer. Don't understand why this is automatically triggered by [tableView reloadData]. Shouldn't this just be a function that could be overiden ? – Benjamin Mar 05 '15 at 10:04
  • 12
    Dude, if you call reloadData, ALL the code before it that does any "reloadSections" and stuff like that become useless. ReloadData reloads EVERYTHING. So you EITHER do a reloadData OR you do reloadSections/reloadRows/etc to cherry pick which parts of your table to reload, never both. – Tom van Zummeren May 14 '15 at 11:22
4

Why do you need to reloadData of your guestList at the end of the function ? You are already reloading the sections you want... Try removing that and check again.

nicolasthenoz
  • 1,852
  • 1
  • 13
  • 14
  • Thanks. I actually tried it without the reloadData before posting. The table view doesn't update properly without it. Are you supposed to use additional code along with reloadSections? I don't understand this animation and I haven't been able to find anything helpful in my searches. – mySilmaril May 17 '12 at 02:24