Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait until a UITableView built-in animation is complete? [duplicate]

 [self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationLeft];
 [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationRight];

In the above code how do I make the second line execute after the animation from the first line has completed?

I tried this...

[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationLeft];
{
    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationRight];
    [self.tableView endUpdates];
}
[self.tableView endUpdates];

and this...

[self.tableView beginUpdates];
{
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationLeft];
}
[self.tableView endUpdates];
[self.tableView beginUpdates];
{
    [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationRight];
}

...but either way the animations are clearly happening at the same time (and really apparent when slow animations is on).

like image 816
Murray Sagal Avatar asked Apr 28 '13 11:04

Murray Sagal


1 Answers

Thank you Iducool for pointing me to the other question.

This worked...

[CATransaction begin];
[CATransaction setCompletionBlock:^{
    [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationRight];
}];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationLeft];

[CATransaction commit];

I didn't seem to require UITableView's beginUpdates and endUpdates.

like image 116
Murray Sagal Avatar answered Nov 17 '22 21:11

Murray Sagal