在官方文档中是这样介绍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这个事务。我们可以通过以下的代码验证这个结论

[CATransaction begin];

[CATransaction setCompletionBlock:^{
// animation has finished
}]; [tableView beginUpdates];
// do some work
[tableView endUpdates]; [CATransaction commit];

这段代码来自stackoverflow,它的作用就是在tableview的动画结束后,执行需要的操作。这段代码好用的原因就是beginUpdates本质上就是添加了一个动画事务,即CATransaction,当然这个事务可能包含许多操作,比如会重新调整每个cell的高度(但是默认不会重新加载cell)。如果你仅仅更改了UITableView的cell的样式,那么应该试试能否通过调用beginUpdates 和 reloadRowsAtIndexPaths 来实现效果,而不是调用tableview的reloadData方法去重新加载全部的cell!

一个例子,带有动画效果的,重新加载部分cell的代码。

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmp] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

下面是一个例子,想实现一个点击cell,cell的高度就变高的效果,就可以在选中方法中设置标志位,来影响代理方法返回的height值,并且在之后调用

  [tableView beginUpdates];
[tableView endUpdates];

没错,他们之间没有代码,算然没代码,这2句话会根据代理方法重新调整cell的高度。下面是调用的效果截图:

iOS UITableView 的beginUpdates和endUpdates的更多相关文章

  1. iOS:UITableView 方法 属性

    参考:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/UITableView_Class/Reference ...

  2. (转)IOS UITableView学习

    转自:http://www.cnblogs.com/smileEvday/archive/2012/06/28/tableView.html          作者:一片枫叶 看TableView的资 ...

  3. iOS: [UITableView reloadData]

    在 iTouch4 或者相差不多的 iPhone 上,不建议在 UIViewController 的 viewWillAppear 的方法中放置 UITableView 的 reloadData 方法 ...

  4. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  5. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  6. iOS UITableView Tips(2)

    #TableView Tips(2) (本来想一章就结束TableView Tips,但是发现自己还是太天真了~too young,too simple) ##架构上的优化 在Tips(1)中指出了一 ...

  7. beginUpdates和endUpdates

    我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...

  8. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  9. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

随机推荐

  1. url的编码问题

    JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...

  2. iOS之单例

    今天在看多线程同步时,突然想到了单例的同步问题.自从dispatch_once出现后,我们创建单例非常简单且安全: static dispatch_once_t pred; static Single ...

  3. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. Serverlet程序

    Serverlet是用Java编写的服务器端程序;主要用于交互地浏览和修改数据,生成动态Web内容; 一个serverlet就是一个继承于HttpServlet抽象类的Java类:下面先看一个简单的例 ...

  5. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  6. 优秀大数据GitHub项目一览

    http://blog.csdn.net/yaoxtao/article/details/50540485 优秀大数据GitHub项目一览 VMware CEO Pat Gelsinger曾说: 数据 ...

  7. java 打包jar文件以在没有安装JDK或JRE的机子上运行

    前言: java号称“一次编译,到处运行”,但这有个前提,那就是你的机子上得安装java环境.对于开发人员或其他一些比较懂计算机的人来说这没什么,但是对于一些不懂计算机的人来说这会很麻烦,他们更希望的 ...

  8. Linux 磁盘分区

    原文地址:http://www.jb51.net/LINUXjishu/57192.html 磁头数(Heads)表示硬盘总共有几个磁头,也就是有几面盘片, 最大为 255 (用 8 个二进制位存储) ...

  9. CSS 仿Excel表格功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Enterprise Library系列文章目录(转载)

    1. Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (初级) 2. Microsoft Enterprise Lib ...