如何给tableview reload的reloadData加上动画

3001人阅读
iOS开发(53)
如果在reloadDate后需要立即获取tableview的cell、高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是有可能出问题的。reloadDate并不会等待tableview更新结束后才返回,而是立即返回,然后去计算表高度,获取cell等。如果表中的数据非常大,在一个run loop周期没执行完,这时,需要tableview视图数据的操作就会出问题了。apple并没有直接提供reloadData的api,想要程序延迟到reloadData结束在操作,可以用以下方法方法1:[self.tableView reloadData];
[self.tableView layoutIfNeeded];
//刷新完成layoutIfNeeded会强制重绘并等待完成。方法2:[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
//刷新完成
});reloadDate会在主队列执行,而dispatch_get_main_queue会等待机会,直到主队列空闲才执行。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:432182次
积分:5029
积分:5029
排名:第3324名
原创:94篇
转载:10篇
译文:11篇
评论:239条
微信:lofocus
如果有问题,可加微信 :D
文章:29篇
阅读:201112
扫描二维码,您的朋友也可以
(4)(2)(1)(1)(1)(3)(4)(1)(4)(1)(5)(6)(10)(7)(2)(4)(4)(1)(2)(1)(1)(1)(3)(2)(1)(2)(5)(3)(3)(2)(3)(3)(1)(2)(2)(2)(1)(5)(2)(1)(2)(1)(2)(1)beginUpdates和endUpdates
实现UITableView的动画块 - 推酷
beginUpdates和endUpdates
实现UITableView的动画块
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作。
(以下是删除动画效果)
这样,我们就会用到
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
[_currentData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
向上面一段代码,就是动态删除UITableView 的UITableViewCell的操作。
所以,就需要使用beginUpdates方法和endUpdates方法,将要做的删除操作“包”起来!
beginUpdates方法和endUpdates方法是什么呢?
这两个方法,是配合起来使用的,标记了一个tableView的动画块。
分别代表动画的开始开始和结束。
两者成对出现,可以嵌套使用。
一般,在添加,删除,选择 tableView中使用,并实现动画效果。
在动画块内,不建议使用reloadData方法,如果使用,会影响动画。
一般什么时候使用这么一个动画块呢?
一般在UITableView执行:删除行,插入行,删除分组,插入分组时,使用!用来协调UITableView的动画效果。
插入指定的行,
在执行该方法时,会对数据源进行访问(分组数据和行数据),并更新可见行。所以,在调用该方法前,应该先更新数据源
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
插入分组到制定位置
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
插入一个特定的分组。如果,指定的位置上已经存在了分组,那么原来的分组向后移动一个位置。
删除制定位置的分组
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
删除一个制定位置的分组,其后面的分组向前移动一个位置。
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
移动原来的分组从一个位置移动到一个新的位置。如果,新位置上若存在某个分组,那这某个分组将会向上(下)移动到临近一个位置。该方法,没有动画参数。会直接移动。并且一次只能移动一个分组。
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致下次自动登录
关注移动互联网和移动APP开发工具、开发框架、测试工具、微信开发、Android源码、Android开源类库以及各种开源组件的IT科技网站
现在的位置:
iOS开发——使用UITableView是如何判断reloadData加载数据已经结束
本文主要讲述了在使用UITableView实现数据,当使用reloadData加载数据时,如何判断该方法已经执行完毕。现在把相关的代码整理出来分享给广大的iOS程序员兄弟们,希望给他们的开发工作带来帮助。
如果在reloadDate后需要立即获取tableview的cell、高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是有可能出问题的。
reloadDate并不会等待tableview更新结束后才返回,而是立即返回,然后去计算表高度,获取cell等。
如果表中的数据非常大,在一个run loop周期没执行完,这时,需要tableview视图数据的操作就会出问题了。
apple并没有直接提供reloadData的api,想要程序延迟到reloadData结束在操作,可以用以下方法
[self.tableView reloadData];
[self.tableView layoutIfNeeded];
//刷新完成
layoutIfNeeded会强制重绘并等待完成。
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
//刷新完成
reloadDate会在主队列执行,dispatch_get_main_queue会等待机会,知道主队列空闲才执行。
【上篇】【下篇】学习总结(3)
[UIView transitionWithView:_table &
& & & & &duration: 0.35f &
& & & & &options: UIViewAnimationOptionTransitionCrossDissolve &
& & & & &animations: ^(void) &
& & & & &{ &
& & & & &[_table reloadData]; &
& & & & &} &
& & & & &completion: ^(BOOL isFinished) &
& & & & &{ &
& & & & & &
& & & & &}]; &
UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionAllowUserInteraction //动画时允许用户交流,比如触摸
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画
UIViewAnimationOptionRepeat //动画无限重复
UIViewAnimationOptionAutoreverse //执行动画回路,前提是设置动画无限重复
UIViewAnimationOptionOverrideInheritedDuration //忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve //忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent //通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionShowHideTransitionViews //用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套继承的选项
//时间函数曲线相关
UIViewAnimationOptionCurveEaseInOut //时间曲线函数,由慢到快
UIViewAnimationOptionCurveEaseIn //时间曲线函数,由慢到特别快
UIViewAnimationOptionCurveEaseOut //时间曲线函数,由快到慢
UIViewAnimationOptionCurveLinear //时间曲线函数,匀速
//转场动画相关的
UIViewAnimationOptionTransitionNone //无转场动画
UIViewAnimationOptionTransitionFlipFromLeft //转场从左翻转
UIViewAnimationOptionTransitionFlipFromRight //转场从右翻转
UIViewAnimationOptionTransitionCurlUp //上卷转场
UIViewAnimationOptionTransitionCurlDown //下卷转场
UIViewAnimationOptionTransitionCrossDissolve //转场交叉消失
UIViewAnimationOptionTransitionFlipFromTop //转场从上翻转
UIViewAnimationOptionTransitionFlipFromBottom //转场从下翻转
 以上是浅略的理解,欢迎朋友有更好的指正,以免误人子弟。
 补充:关于最后一组转场动画它一般是用在这个方法中的:
    [UIView transitionFromView: toView: duration: options: completion:^(BOOL finished) {}];
 该方法效果是插入一面视图移除一面视图,期间可以使用一些转场动画效果。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:69次
排名:千里之外12334人阅读
MacOSX(18)
分类:&&1054人阅读&&&
UITableView reloadData的正确方法。& &
相信很多人会遇到这种情况,当tableView正在滚动的时候,如果reloadData,偶尔发生App&crash的情况。 这种情况有时候有,有时候没有,已经难倒了很多人。直至今天,我在stackoverflow上面,仍没有发现真正有说到其本质的帖子。我的处女贴,选择这个问题来阐述一下我的观点。
小弟我英语很好,一般都是用英语记笔记,当然,我知道,论坛愤青很多,如果只贴英文出来,肯定找骂。 故简单翻译一下,以显示我的诚意。 原英文笔记附在后面。 请大家不要挑英语语法错误了,笔记就是笔记,不是出书。&
第一句话,阐述问题的本质:在tableView的dataSource被改变 和 tableView的reloadData被调用之间有个时间差,而正是在这个期间,tableView的delegate方法被调用,如果新的dataSource的count小于原来的dataSource count,crash就很有可能发生了。
下面的笔记提供了两种解决方案,和记录了一个典型的错误,即 在background thread中修改了datasource,虽然调用&[self.tableView&performSelectorOnMainThread:@selector(reloadData)&withObject:nilwaitUntilDone:NO];&
记住正确的原则:&Always change the dataSource&and(注意这个and)&reloadData in the mainThread. What's more, reloadData should be called&immediately&after the dataSource change.&
If dataSource is changed but tableView's reloadData method is not called immediately, the tableView may crash if it's in scrolling.&
Crash Reason:&There is still a time gap between the dataSource change and reloadData. If the&table&is scrolling during the time gap, the app may Crash!!!!
WRONG WAY:&
Following codes is WRONG: even the reloadData is called in main thread, there is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
wrong codes samples:&
-(void) changeDatasource_backgroundThread
@autoreleasepool{
[self.dataSourceArray&removeAllObjects];&
[self.tableViewperformSelectorOnMainThread:@selector(reloadData)&withObject:nil&waitUntilDone:NO];
RIGHT WAY:&
Principle: &Always change dataSource in&MAIN&thread and call the reloadData&immediately&after it.&
Option 1:&If the operation to change the dataSource should be executed in background, the operation can create a temp dataSource array and pass it to main thread with notification, the main thread observes the notification, &assign
the tmpDataSource to dataSource and reload the tableView by reloadData.
Option 2:&In the background, call the GDC dispatch_async to send the two methods to main thread&together.
dispatch_async(dispatch_get_main_queue(), ^{
& & & &&self.dataSourceArray= a new Array.
& & & & [self.tableView reloadData];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:644244次
积分:8883
积分:8883
排名:第1233名
原创:218篇
转载:219篇
评论:87条
(6)(4)(1)(1)(5)(1)(7)(13)(12)(11)(4)(9)(3)(8)(4)(6)(3)(1)(2)(4)(2)(2)(24)(7)(9)(44)(2)(1)(2)(2)(3)(6)(14)(23)(3)(6)(6)(6)(2)(8)(12)(10)(19)(18)(19)(20)(28)(24)(7)(6)

我要回帖

更多关于 easyui table reload 的文章

 

随机推荐