在官方文档中是这样介绍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. nmap常用命令

    1) 获取远程主机的系统类型及开放端口 nmap -sS -P0 -sV -O <target> 这里的 < target > 可以是单一 IP, 或主机名,或域名,或子网 - ...

  2. DLX模型问题

    问题:sevenzero liked Warcraft very much, but he haven't practiced it for several years after being add ...

  3. (BZOJ4538)HNOI2016 网络

    HNOI2016 Day1 T2 网络 Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数 ...

  4. [NOIP2008] 提高组 洛谷P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  5. HDU1698 Just a Hook

    Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  6. 最短路之Dijkstra算法

    1. 邻接矩阵 int cost[MAX_V][MAX_V]; //assume cost[u][v]>0 int d[MAX_V]; bool used[MAX_V]; void Dijkst ...

  7. Linux命令详解:[7]获得命令帮助

    在维护和使用Linux系统时,常常会忘记命令的使用方法,如果旁边又没有相应的资料,那怎么办呢?不用担心,系统本身提供了详细的手册供使用者查询,下面小编就以CentOS6.4系统为例演示如何获得命令帮助 ...

  8. go runtime scheduler

     http://www.slideshare.net/matthewrdale/demystifying-the-go-scheduler http://www.cs.columbia.edu/~a ...

  9. mysql 出现错误Incorrect file format

    REPAIR TABLE xx_messages USE_FRM;

  10. apache安装后编译新模块

    1.下载对应版本的源码包 2.解压后找到modules/mappers目录并进入 3.运行如下命令自动编译.安装和修改httpd.conf文件: /usr/sbin/apxs -c -i -a mod ...