UITableView beginUpdates/endUpdates 回调

Posted

技术标签:

【中文标题】UITableView beginUpdates/endUpdates 回调【英文标题】:UITableView beginUpdates/endUpdates callback 【发布时间】:2012-01-09 20:09:13 【问题描述】:

我正在使用 beginUpdates/endUpdates 块对 tableView 进行更改。在整个过程中,我需要更新一个投影,以便它反映 tableView 的当前组合。

我尝试为 tableView 的contentSize 设置 KVO,但只有在动画完成后才会在 endUpdates 上调用它。我想要的是每次contentSize 更改时都会调用它(即使它只有一个像素)。有什么方法可以实现吗?

【问题讨论】:

【参考方案1】:

这个怎么样?

[CATransaction begin];

[CATransaction setCompletionBlock:^
    // animation has finished
];

[tableView beginUpdates];
// do some work
[tableView endUpdates];

[CATransaction commit];

【讨论】:

哇!这几乎可以工作,但是在表格视图结束更新和完成块运行之间存在延迟。现在我将使用它。谢谢。【参考方案2】:

鲁道夫的方法对我来说并没有预期的那么顺利。在我的例子中,我使用它在UITableView 上选择了一行,而 Rudolf 的方法是让表格做两个动画,但有一点冻结:beginUpdates/endUpdates 中的动画、一点冻结和完成块上的动画。

 [tableView selectRowAtIndexPath:indexPath
                        animated:YES
                  scrollPosition:scrollPosition];

这激发了我创建此代码...并且无缝运行:

[UIView animateWithDuration:0.0 animations:^
    [tableView beginUpdates];
    // do something to the table
    [tableView endUpdates];
 completion:^(BOOL finished) 
    // Code to run when table updates are complete.
];

【讨论】:

【参考方案3】:

很抱歉,我认为你不能做到这一点。当您在调用beginUpdates 之后对表格进行更改时,这些更改将作为endUpdates 之后的单个动画进行动画处理。这些动画期间没有动画回调。我没有尝试过,所以不知道它是否适用,但您可以尝试嵌套 beginUpdatesendUpdates 并在每个 endUpdates 之后更新您的影子。

【讨论】:

仅供参考:beginUpdates 和 endUpdates 的调用可以嵌套。如果您不在此块内进行插入、删除和选择调用,则行数等表属性可能会失效。 你有一个应该如何使用嵌套调用的例子吗?

以上是关于UITableView beginUpdates/endUpdates 回调的主要内容,如果未能解决你的问题,请参考以下文章

执行“beginUpdates”“endUpdates”时 UITableView 页脚视图的动画问题

UITableView 意外反弹 beginUpdates()/endUpdates()/performBatchUpdates()

UITableView beginUpdates/endUpdates 回调

UITableView "beginUpdates" 和 UICollectionView "performBatchUpdates" 是不是具有相同的行为?

beginUpdates和endUpdates 实现UITableView的动画块

UITableView beginUpdates/endUpdates 同时滚动使滚动跳转