美文网首页UITableViewiOS开发iOS开发常用知识点
浅谈TableView的begin Updates和end Up

浅谈TableView的begin Updates和end Up

作者: Qing学 | 来源:发表于2017-11-12 16:07 被阅读0次
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

相关文章

  • 浅谈TableView的begin Updates和end Up

    实现效果如下 通过tableView的reloadData方法我们可以方便的对tableVie的cell根据数据源...

  • end和begin

    国庆节,七天假,一晃就过完了. 第一部分: 假期乐曲是这样演绎: 九月的最后一天,开始雀跃. 假期第二天,还是好开...

  • STL-sort

    ·包含头文件 升序:sort(begin,end,less ());降序:sort(begin,end,great...

  • 2019-05-28 ios touch触摸链

    1、UIView,begin向上传递begin-》beign-〉...-》end-〉end 2、UIContol,...

  • End And Begin

    有些故事还没讲完那就算了吧那些心情在岁月中已经难辨真假如今这里荒草丛生没有了鲜花好在曾经拥有你们的春秋和冬夏 早就...

  • end or begin

    不想整理过去的12年 琐碎的不完整的未完成的 都装进信封里,寄给需要的时间中的人。 脑袋被不断更新后,真的会释怀好...

  • The end The begin

    The end 终于顺利走过了2016年的第一个四十九天。回想初衷,是为了给自己制造一个开始的时间点,非常感谢...

  • Begin with the end

    “以终为始” (Begin with end in mind)是7 habits 的第二个习惯。可以指导生活,工作...

  • 小算法

    1、二分搜索auto begin=container.begin(),end=container.end();au...

  • MATLAB之CVX的一些简单笔记

    1 cvx_begin和cvx_end 所有的cvx建模必须以cvx_begin开始以cvx_end结束,所有的变...

网友评论

    本文标题:浅谈TableView的begin Updates和end Up

    本文链接:https://www.haomeiwen.com/subject/aponmxtx.html