浅谈TableView的begin Updates和end Updates

image.png

tableViewBeginAnimation.gif

实现效果如下

通过tableView的reloadData方法我们可以方便的对tableVie的cell根据数据源进行刷新。但是这种刷新方法在某些时候也不是那么合适。比如只需要更新几行的时候可能显得多余。同时在tableView较为复杂的时候还会产生性能的问题。在这种时候我们可以使用tableView的begin Updates方法和end Updates方法来对tableView的几行数据进行增删改和对cell的位置移动。系统会自动给我们的操作加上动画。
一.TableView进行插入cell操作
1.只进行tableView的插入代码如下

[self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        [self.arrayData insertObject:@"新添加的行" atIndex:0];
        [self.tableView endUpdates];
        [CATransaction commit];

2.添加Transaction事务代码如下

[CATransaction begin];
        [CATransaction setCompletionBlock:^{
            NSLog(@"插入cell完成");
        }];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        [self.arrayData insertObject:@"新添加的行" atIndex:0];
        [self.tableView endUpdates];
        [CATransaction commit];

3.设置动画延时、持续时间、并且设置动画类型可以使用如下代码

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            NSLog(@"插入cell完成");
        }];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        [self.arrayData insertObject:@"新添加的行" atIndex:0];
        [self.tableView endUpdates];
        [CATransaction commit];
    } completion:^(BOOL finished) {
        NSLog(@"动画执行完毕");
    }];

同时代码的执行顺序如下

2017-11-12 15:54:14.219972+0800 TableViewBeginEndUpdates[2400:84658] 插入cell完成
2017-11-12 15:54:14.220563+0800 TableViewBeginEndUpdates[2400:84658] 动画执行完毕

二、tableView进行删除cell操作

[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            
        }];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        [self.arrayData removeObjectAtIndex:0];
        [self.tableView endUpdates];
        [CATransaction commit];
    } completion:^(BOOL finished) {
        
    }];

三、tableView进行修改cell操作

[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAutoreverse animations:^{
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            
        }];
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        [self.arrayData replaceObjectAtIndex:0 withObject:@"修正后的cell"];
        [self.tableView endUpdates];
        [CATransaction commit];
    } completion:^(BOOL finished) {
        
    }];

四、tableView进行移动cell操作

[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
    } completion:^(BOOL finished) {
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            
        }];
        [self.tableView beginUpdates];
        [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        NSString *str = self.arrayData[0];
        [self.arrayData removeObjectAtIndex:0];
        [self.arrayData insertObject:str atIndex:2];
        [self.tableView endUpdates];
        [CATransaction commit];
    }];

本示例demo地址为:https://github.com/wangqingxue/TableViewBeginEndUpdates

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,008评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 万圣节是每年西方国家的传统节日。 这一夜是一年中人们认为会“闹鬼”的一夜,所以也叫“鬼节”。 万圣节是诸圣节(Al...
    人力资源管理学习与观察阅读 990评论 0 1
  • 车在路上跑,所以要投保,车跑人也跑,为何人不保,车坏可以修,人坏把命丢,留债不留爱,你说坏不坏,老婆没人爱,儿女无...
    dcbbb8c129a4阅读 278评论 0 0
  • 不过,我开始相信一切都会变好的。不是因为我乐观还是怎么的,就是如果有心理暗示的作用,相信事情会变好,比相信事情不会...
    茶也不甜阅读 165评论 0 0