iOS UITableView 的beginUpdates和endUpdates
在官方文档中是这样介绍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的更多相关文章
- iOS:UITableView 方法 属性
参考:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/UITableView_Class/Reference ...
- (转)IOS UITableView学习
转自:http://www.cnblogs.com/smileEvday/archive/2012/06/28/tableView.html 作者:一片枫叶 看TableView的资 ...
- iOS: [UITableView reloadData]
在 iTouch4 或者相差不多的 iPhone 上,不建议在 UIViewController 的 viewWillAppear 的方法中放置 UITableView 的 reloadData 方法 ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- iOS UITableView Tips(2)
#TableView Tips(2) (本来想一章就结束TableView Tips,但是发现自己还是太天真了~too young,too simple) ##架构上的优化 在Tips(1)中指出了一 ...
- beginUpdates和endUpdates
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...
- iOS UITableView 与 UITableViewController
很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
随机推荐
- JS模式:Mixin混合模式,=_=!就是常见的Object.create()或者_extend()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- git 的简单使用方法
git 的简单使用方法1. 服务器 安装完成2. ssh 中的账号创建完成3. 创建 ssh 账号,会在 ssh 的安装目录下的home 目录里面,多了用户家目录4. 进入该目录 ,创建一个新的文件夹 ...
- selenium ide 录制回放link链接报错
回放是出现以下错误: 也就是回放点击打开新的链接时出现这个错误, 这个问题说的是 点击此链接会新打开一个窗口 selenium1是不支持多窗口切换的 因此会卡在这里,也就录制不支持这个操作,但是很多书 ...
- 部署搭建 Saltstack(centos6.6)
SaltStack介绍 官网:https://docs.saltstack.com/en/latest/ 中国saltstack用户组http://www.saltstack.cn/ 下图是它的子系统 ...
- Laravel教程 六:表单 Forms
Laravel教程 六:表单 Forms 此文章为原创文章,未经同意,禁止转载. Form laravel 5.2 之后请使用 laravelcollective/html 替换 illuminate ...
- JavaScript常见调试方法
编辑导语:javascript调试方法,常见使用alert和console来定位出错和输出的结果是否是想要的,在chrome中,还可以使用断点来看运行的情况等,本文介绍了比较全面的调试方法,你知道co ...
- Circular Sequence,ACM/ICPC Seoul 2004,UVa 1584
#include <stdio.h> #include <string.h> #define maxn 105 int lss(const char *s,int p,int ...
- include(thinkphp常用内置标签)
变量输出使用普通标签就足够了,但是要完成其他的控制.循环和判断功能,就需要借助模板引擎的标签库功能了,系统内置标签库的所有标签无需引入标签库即可直接使用. XML标签有两种,包括闭合标签和开放标签,一 ...
- NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager 解决方法
差一个jar包, 将hibernate-commons-annotations.jar加入到classpath中
- commons-logging和Log4j 日志管理/log4j.properties配置详解
commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...