美文网首页日常杂货铺算法或者代码iOS
tableViewCell的依次插入动画

tableViewCell的依次插入动画

作者: 大墙66370 | 来源:发表于2016-06-26 01:45 被阅读2381次

如果有一个小需求当我们的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

相关文章

网友评论

  • 国王or乞丐:我这边没实现这个动画效果,因为是混编,数组是nsarray声明的,然后我这边在定时器中加上了两句话,就是动画插入那两句,还是不行,不知道楼主是不是可以加个好友,帮忙定位下是哪里的问题呢,我这边可以给你截图,如果不喜欢加陌生人的话,帮忙看过问题,可以删除我的,谢谢了
    大墙66370:@国王or乞丐 718224580 q
  • 91d311374b79:怎么只显示一行数据了,有demo么
    大墙66370:@别太放肆 我重新粘贴复制一遍,可以啊。
    91d311374b79:@大墙66370 就是复制粘贴的 分组不分组都是显示一条。
    大墙66370:@别太放肆你复制粘贴一下呗代码也不多
  • 路路有话说:后面加上table view.tablefootview = uiview new
    效果更好谁用谁知道
    FDZero:@sololi :smile:
  • ROOKIE:可以不用定时器的 只要
    下面一个比上面一个从更远的地方飞过来就好了
  • maguns:感谢楼主分享,有启发
  • 东方_未明:感觉在cellWillDisplay中做个动画就可以实现,而且更简单
    MccReeee:这个主意也不错
    抢手的哥:@东方_未明 bingo
  • 萧子然:前排捡分

本文标题:tableViewCell的依次插入动画

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