UITableView的 beginUpdates 和 endUpdates<转>

先看Apple API Reference中对这两个方法的描述

beginUpdates

endUpdates

从上述描述中我们大概可以总结出四点

1、beginUpdates 和 endUpdates必须成对使用

2、使用beginUpdates和endUpdates可以在改变一些行(row)的高度时自带动画,并且不需要Reload row(不用调用cellForRow,仅仅需要调用heightForRow,这样效率最高)。

3、在beginUpdates和endUpdates中执行insert,delete,select,reload row时,动画效果更加同步和顺滑,否则动画卡顿且table的属性(如row count)可能会失效。

4、在beginUpdates 和 endUpdates中执行 reloadData 方法和直接reloadData一样,没有相应的中间动画。



针对上面几点举几个栗子

1、改变Row的高度

直接调用

[self.tableViewbeginUpdates];

[self.tableViewendUpdates];

接着tableview回调-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath,调整每一行的高度。

2、同时insert,delete,select reload

[self.tableViewbeginUpdates];

[self.testArrayinsertObject:@(-1)atIndex:0];

[self.tableViewinsertRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:0inSection:0]]withRowAnimation:UITableViewRowAnimationAutomatic];

[self.testArrayremoveObjectAtIndex:3];

[self.tableViewdeleteRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:2inSection:0]]withRowAnimation:UITableViewRowAnimationAutomatic];

[self.tableViewselectRowAtIndexPath:[NSIndexPathindexPathForRow:3inSection:0]animated:YESscrollPosition:UITableViewScrollPositionMiddle];

[self.tableViewreloadRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:4inSection:0]]withRowAnimation:UITableViewRowAnimationAutomatic];

[self.tableViewendUpdates];

如何在做完动画之后执行某个方法呢?

[CATransactionbegin];

[CATransactionsetCompletionBlock:^{

NSLog(@"aferCompletionBlock");

}];

[self.tableViewbeginUpdates];

[self.tableViewendUpdates];

[CATransactioncommit];

如何控制动画的执行时间呢?

[UIViewanimateWithDuration:2.0fdelay:0.0options:UIViewAnimationOptionCurveEaseInOutanimations:^{

[CATransactionbegin];

[CATransactionsetCompletionBlock:^{

NSLog(@"after2");

}];

[self.tableViewbeginUpdates];

[self.tableViewendUpdates];

[CATransactioncommit];

NSLog(@“after1”);

}completion:^(BOOLfinished) {

NSLog(@"after3");

}];

输出顺序: after1->after2->after3

利用UIView的动画的执行时间来控制beginUpdates和endUpdates的动画时间。

时间: 2024-08-08 17:10:33

UITableView的 beginUpdates 和 endUpdates<转>的相关文章

iOS开发-beginUpdates &amp;&amp; endUpdates用法

本篇主要介绍使用beginUpdates和endUpdates方法对UITableView的Cell进行批量操作更新.首先给出过程中依赖的数据源: self.arraySections = [NSMutableArray arrayWithCapacity:0]; NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"Apple",@"Alice",@"Apache",@"

UITableView出现卡顿如何处理

tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画block,图片加载的时候显的很平滑.你自己试一下就知道了. 加载图片的时候要用多线程,要用缓存,也就是需要异步加载 计算cell的高度的时候要尽量的简单,因为tableVIew中cell的高度是一次性加载完的 要用重用机制,一定要用,不然会卡的 用户习惯性快速的滚动,视图和数据内容都会快速的变化,如果

UITableView的简单总结与回顾

今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会从九个方面对这个控件做一个简单的综述,希望对大家有帮助吧,嘿嘿. 一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示:          其中左边的是Plain风格的,右边的是Grouped风格,这个区别

UITableView 常用方法(转自CSDN)

一.概述 UITableView是iOS开发比不可少也是最重要的一个控件类.可以说任何一个做iOS开发的人都必须熟练使用和掌握它.本文主要就是提供一个学习使用TableView的指南. 要说UITableView必须要介绍他的几个亲戚:UITableViewDelegate,UITableViewDataSource,UITableViewCell.其中前两个是TableView遵守的两个protocol(别告诉我你不知道啥叫protocol哦).然后本文会再列出TableView最常用最重要的

UITableView使用指南

1) 初始化 UITableView对象 – initWithFrame:style: // 代码生成方式,如果你在nib里加的tableview不需要使用这个方法 2)配置TableView – dequeueReusableCellWithIdentifier: // 必须要实现的方法,与TableView同生同死 style property // 有两种 UITableViewStylePlain, UITableViewStyleGrouped,经常用 – numberOfRowsIn

UITableView / UITableViewDataSource / UITableViewDelegate

一张图解释TableView各属性 0 UITableView Initializing a UITableView Object 初始化: - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style Configuring a Table View 配置: 1. style /** TableView样式,默认Plain */ @property(nonatomic, readonly) UITableVi

UITableView的编辑(插入、删除、移动)

先说两个方法beginUpdates和endUpdates,几点注意事项: 一般我们把行.块的插入.删除.移动写在由这两个方法组成的函数块中.如果你不是在这两个函数组成的块中调用插入.删除.移动方法,表的属性(比如行数)可能失效. 一般也不应该在由这两个函数组成的函数块中调用reloadData,如果你这么做了,那么所有的动画都要自己进行. 这两个方法组成的块,可以嵌套. 同一个块中的插入.删除操作,先处理完删除操作才会执行插入操作,而不管在它们在块中的顺序. UITableView是否处于编辑

UITableView:可展开的 UITableView

针对 TableView,有些时候需要在点击 cell 时,展开这行 cell,显现出更多的选项或者全部内容等. 比较容易想到的处理方案就是利用 section,在未选择之前,每一行都是一个 section,待展开的内容都在这个 section 的 row 里面.在点击事件里,reload 当前选中的 section,在 numberOfRows 方法中,返回大于 0 的数量. 这里谈谈另外一种很简单的方法,直接在 TableViewCell 上面做文章. 1.点击 cell 时,在 didSe

UITableView 的一些奇淫技巧1

http://www.strongx.cn/  感谢这位老兄 UITableView是工程开发中最经常使用到的UI控件,但是你真的了解它嘛,这里记录几点有用的但你可能并不知道的. 当我们的数据未能显示满一屏幕的时候,UITableView self.tableView.tableFooterView = [[UIView alloc]init]; UITableView1515 self.tableView.separatorInset = UIEdgeInsetsZero; 但是你很快就会发现