tableViewCell的依次插入动画

如果有一个小需求当我们的tableView不是直接加载到界面上,而是cell依次展示到我们的界面上只一个动画效果,我们需要怎嘛做呢?
其实很简单,我们数据源有多少 我么就依次 插入多少个cell 不就行了吗? 需要注意的地方时是,当我们要插入的时候 要保证,下面返回的cell个数是对的就行了呗.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.DatNum+1;   
}

下面是我的实现 直接拷贝代码沾到你的 ViewController.m里就行了

//  ViewController.m
//  tableViewCellAnimation
//
//  Created by 3D on 16/6/24.
//  Copyright © 2016年 3D. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()   <UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,strong)NSMutableArray *indesPaths;
@property (nonatomic,assign)int DatNum;
@property(nonatomic,strong) NSTimer * timer;
@end

@implementation ViewController

-(UITableView *)tableView{
if (!_tableView) {
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
 return _tableView;
}
-(NSArray *)dataArr{
if (!_dataArr) {
    _dataArr = @[@"我是个1",
                 @"我是个2",
                 @"我是个3",
                 @"我是个4",
                 @"我是个5",
                 @"我是个6",
                 @"我是个7",
                 @"我是个8",
                 @"我是个9",
                 @"我是个10",
                 @"我是个11",
                 @"我是个12",
                 @"我是个13",
                 @"我是个14",
                 @"我是个15",
                                   ];
      }
  return _dataArr;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.tableView];
self.DatNum = -1;
    NSMutableArray *indexPaths = @[].mutableCopy;
    self.indesPaths = indexPaths;
   self.timer =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(charusell) userInfo:nil repeats:YES];  
 }

-(void)charusell{
self.DatNum = self.DatNum +1;
if (self.DatNum < self.dataArr.count) {
    [self.indesPaths addObject:[NSIndexPath indexPathForItem:self.DatNum inSection:0]];
    [self.tableView insertRowsAtIndexPaths:self.indesPaths withRowAnimation:UITableViewRowAnimationRight];
    [self.indesPaths removeAllObjects];
}else{
    [self.timer invalidate];
   //记得当不用这个定时器的时候要销毁. 
   self.timer = nil;
   }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.DatNum+1;   
}

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.dataArr[indexPath.row];

return cell;
 }
@end

效果如下

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

推荐阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,378评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 块与大中枢派发 所谓的快和大中枢派发,就是我们常说的block和GCD 37. 理解“块”这一概念 快与函数类似,...
    Miridescent阅读 333评论 2 5
  • 周末回家,一路上满脑子都是你们。心不自觉的激动起来,脚步更快了,愈近愈觉得心里满满的憧憬。刚进门,俩宝宝坐在床上,...
    时慧慧爱物阅读 311评论 0 1
  • 很开心,自己终于坚持到第三天了,请继续加油写作。这其中最大的阻碍,来自我家的猫,522。我家没有书房,所以只能在客...
    Ivy0927阅读 193评论 0 0