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


文/大墙66370(简书作者)
原文链接:http://www.jianshu.com/p/ff0db8fafd0c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值