zoukankan      html  css  js  c++  java
  • 动态改变cell的高度&beginUpdates和endUpdates-实现UITableView的动画块

    1.利用reloadRowsAtIndexPaths:withRowAnimation:来动态改变cell的高度

    UITableView的- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    这一方法会重新加载所指定indexPaths中的UITableViewCell实例,因为重新加载cell所以会请求这个UITableView实例的data source来获取新的cell;这个表会用动画效果让新的cell进入,并让旧的cell退出。

    会调用UITableViewDataSource协议中的所有方法来更新数据源,其中调用 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    只会调用所需更新的行数,来获取新的cell,

    注意:此时该cell的- (void)setSelected:(BOOL)selected animated:(BOOL)animated将被调用,所设置的selected为NO;

     
    2.我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作。
    这样,我们就会用到

     [tableView beginUpdates];

            if (newCount<=0) {

                [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];

            }

            

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];

            [tableView endUpdates];

     
    向上面一段代码,就是动态删除UITableView 的UITableViewCell的操作。
    因为,如果我们的UITableView是分组的时候,我们如果删除某个分组的最后一条记录时,相应的分组也将被删除。所以,必须保证UITableView的分组,和cell同时被删除。
    所以,就需要使用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

    移动原来的分组从一个位置移动到一个新的位置。如果,新位置上若存在某个分组,那这某个分组将会向上(下)移动到临近一个位置。该方法,没有动画参数。会直接移动。并且一次只能移动一个分组。

     
    在如上方法中,建议使用该动画块进行操作!
     
    希望对你有所帮助!
  • 相关阅读:
    【leetcode】1295. Find Numbers with Even Number of Digits
    【leetcode】427. Construct Quad Tree
    【leetcode】1240. Tiling a Rectangle with the Fewest Squares
    【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
    【leetcode】1291. Sequential Digits
    【leetcode】1290. Convert Binary Number in a Linked List to Integer
    【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
    【leetcode】1289. Minimum Falling Path Sum II
    【leetcode】1288. Remove Covered Intervals
    【leetcode】1287. Element Appearing More Than 25% In Sorted Array
  • 原文地址:https://www.cnblogs.com/zhaohanjun/p/4720524.html
Copyright © 2011-2022 走看看