zoukankan      html  css  js  c++  java
  • 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的动画时间。

  • 相关阅读:
    【华为云技术分享】40行代码教你利用Python网络爬虫批量抓取小视频
    【华为云技术分享】【Python成长之路】机器学习:10+分类算法汇总学习
    【华为云技术分享】漫谈LiteOS之开发板-中断(基于GD32450i-EVAL)
    【华为云技术分享】多模态融合算法——Multimodal Compact Bilinear Pooling
    【Java_基础】HashMap的工作原理
    【前端_js】jQuery动态设置元素的样式
    【前端_React】npm常用命令
    【前端_React】Node.js和webpack的安装
    【Java框架】Postman的使用
    【前端_js】javascript中数组的map()方法
  • 原文地址:https://www.cnblogs.com/deng37s/p/6933086.html
Copyright © 2011-2022 走看看