Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell When a UITableView animation has finished?

How can I tell when [UITableView setEditing:YES animated:YES] has completed?

I don't want to give any context, because I want to avoid anybody giving me workarounds, which do not interest me.

What I want is to call the above, then have a separate function called when the animation is completed.


I am edited the post to give context and some workarounds. Originally I setEditing and immediately reload the table data.

[tableView setEditing:YES animated:YES];
[tableView reloadData];

The problem is that the table reloads before the animation begins, and so the animation is never seen.

Here are some various workarounds:

[tableView setEditing:YES animated:YES];
[self performSelector:@selector(ReloadTable) withObject:nil afterDelay:1.0];

This works but if I get the delay incorrect then it will look bad. So I need to know what the delay is, which I can figure out, but we are not gauranteed that the delay will always be the same.

isEditing = YES;
[tableView reloadData];
[tableView setEditing:YES animated:YES];

This could work, but the table behaves differently depending on if we are in editing mode. So I have to use my own isEditing variable instead of the standard UITableView.editing. I would rather not have to create a new boolean isEditing variable.

[tableView setEditing:YES animated:YES];
[tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationTop];

This almost works well but in editing mode the first row should have the UITableViewCellEditingStyleInsert, while the other rows get UITableViewCellEditingStyleDelete. And with the above code the editing style gets set BEFORE the row is added. Therefore the second row ends up with UITableViewCellEditingStyleInsert.

like image 709
jriggs Avatar asked Aug 26 '11 00:08

jriggs


3 Answers

[CATransaction begin];
[CATransaction setCompletionBlock: ^{
    // your animation has finished
}];
[tableView setEditing:YES animated:YES];
[CATransaction commit];

Note that setCompletionBlock must be on the top.

like image 111
Rudolf Adamkovič Avatar answered Nov 09 '22 21:11

Rudolf Adamkovič


In iOS 4 you can do the following:

[UIView animateWithDuration:0.3f
                 animations:^{
                     [self.tableView setEditing:YES animated:NO];
                 } 
                 completion:^(BOOL finished){
                     // Do something
                 }
];
like image 3
Jesse Avatar answered Nov 09 '22 23:11

Jesse


Swift 4 version of accepted answer:

CATransaction.begin()
CATransaction.setCompletionBlock {
    // your animation has finished
}
tableView.setEditing(true, animated: true)
CATransaction.commit()
like image 3
Mohit Singh Avatar answered Nov 09 '22 22:11

Mohit Singh