首页 > 其他 > 详细

iOS UITableView 的beginUpdates和endUpdates

时间:2014-02-06 13:03:37      阅读:368      评论:0      收藏:0      [点我收藏+]

在官方文档中是这样介绍beginUpdates的

Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: andindexPathsForVisibleRows) to be animated simultaneously. This group of methods must conclude with an invocation ofendUpdates. These method pairs can be nested.

If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid. ---------这句话没懂

You should not call reloadData within the group; if you call this method within the group, you will need to perform any animations yourself.

 

一般当tableview需要同时执行多个动画时,才会用到beginUpdates函数,它的本质就是建立了CATransaction这个事务。我们可以通过以下的代码验证这个结论

bubuko.com,布布扣
[CATransaction begin];

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

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

[CATransaction commit];
bubuko.com,布布扣

这段代码来自stackoverflow,它的作用就是在tableview的动画结束后,执行需要的操作。这段代码好用的原因就是beginUpdates本质上就是添加了一个动画事务,即CATransaction,当然这个事务可能包含许多操作,比如会重新调整每个cell的高度(但是不会重新加载cell)。如果你仅仅更改了UITableView的cell的样式,那么应该试试能否通过调用beginUpdates来实现效果,而不是调用tableview的reloadData方法!比如,想实现一个点击cell,cell的高度就变高的效果,就可以在选中方法中设置标志位,来影响代理方法返回的height值,并且在之后调用

  [tableView beginUpdates];
  [tableView endUpdates];

没错,他们之间没有代码,算然没代码,这2句话会根据代理方法重新调整cell的高度。

bubuko.com,布布扣

iOS UITableView 的beginUpdates和endUpdates

原文:http://www.cnblogs.com/breezemist/p/3538673.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!