开发者

How can I tell When a UITableView animation has finished?

开发者 https://www.devze.com 2023-03-31 09:17 出处:网络
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.

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 differe开发者_Go百科ntly 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.


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

Note that setCompletionBlock must be on the top.


In iOS 4 you can do the following:

[UIView animateWithDuration:0.3f
                 animations:^{
                     [self.tableView setEditing:YES animated:NO];
                 } 
                 completion:^(BOOL finished){
                     // Do something
                 }
];


Swift 4 version of accepted answer:

CATransaction.begin()
CATransaction.setCompletionBlock {
    // your animation has finished
}
tableView.setEditing(true, animated: true)
CATransaction.commit()
0

精彩评论

暂无评论...
验证码 换一张
取 消