美文网首页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

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