如何等待UITableView内置动画完成?

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

在上述代码中,我该如何使第二行在第一行的动画完成后执行?
我尝试了以下方法...
[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];

并且这个...

[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];
}

...但不管怎样,动画显然是同时发生的(当慢动画开启时尤为明显)。


1
你可以从这里获取帮助:https://dev59.com/Im865IYBdhLWcg3wZdxW - Iducool
看起来是这样做的:https://dev59.com/YU3Sa4cB1Zd3GeqPxbEA - Dev2rights
1个回答

26

感谢Iducool指引我到另一个问题。

这个有效...

[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];

我似乎不需要使用UITableViewbeginUpdatesendUpdates方法。


4
如果加载的单元格包含 UIActivityIndicatorView,则完成块永远不会被调用。 - markturnip
4
在iOS 9上,使用reload/insertSections对我没有起作用。 - tettoffensive
@markturnip,如果您在单元格中有一个活动指示器视图,您是否找到了解决方案?我在这里提出了这个问题。http://stackoverflow.com/questions/39672868/reloading-table-view-cells-with-an-activity-indicator-view - Berry Blue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接