Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with reloadSections:withRowAnimation animation

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.

like image 679
mySilmaril Avatar asked Dec 28 '22 00:12

mySilmaril


2 Answers

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];
like image 160
mySilmaril Avatar answered Jan 08 '23 09:01

mySilmaril


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.

like image 33
nicolasthenoz Avatar answered Jan 08 '23 07:01

nicolasthenoz