前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >效果类似于label从下往上滑(采用uiTableView实现)

效果类似于label从下往上滑(采用uiTableView实现)

作者头像
用户1219438
发布2018-02-01 15:30:19
1K0
发布2018-02-01 15:30:19
举报
文章被收录于专栏:AliceAlice

首先附上效果图

进行描述一下:效果就是类似于是一个竖直方向的滚动视图 并且方向是从下往上  并且能够一直这样循环下去。

代码“

代码语言:javascript
复制
//
//  ViewController.m
//  demo滚动视图上下
//
//  Created by TaoLi on 16/2/24.
//  Copyright © 2016年 TaoLi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *showTableView;
@property(nonatomic,strong)NSMutableArray *shouDatas;
@property(nonatomic,strong)UIView *testView;
@property(nonatomic,assign)CGFloat count;
@property(nonatomic,strong)NSTimer *myTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //对数据进行设置
    _shouDatas = [[NSMutableArray alloc]init];
    for(int i = 0;i<5;i++)
    {
        [self.shouDatas addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    
    //对tableview进行设置
    _showTableView = [[UITableView alloc]initWithFrame:CGRectMake(100, 100,200 , 44)];
    [self.showTableView setSeparatorColor:[UIColor blueColor]];
    //[self.showTableView setSeparatorStyle:];
    self.showTableView.delegate = self;
    self.showTableView.dataSource = self;
    [self.view addSubview:self.showTableView];

    
    self.myTimer =  [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scroll:) userInfo:nil repeats:YES];
    
    self.count = 0;
    //self.sumCount =[UIScreen mainScreen].bounds.size.height/40;

}
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
{
    [self.showTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

//设置每行的单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
    static NSString *reuseIdentifier  = @"Cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    //2.如果没找到,自己创建单元格对象
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }
    cell.textLabel.text = self.shouDatas[indexPath.row];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shouDatas.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
#pragma mark-定时器的实现方法
-(void)scroll:(NSTimer*)sender
{
    if(self.shouDatas.count==self.count)
    {
        self.count=0;
    
        [self.showTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                                        animated:NO
                                  scrollPosition:UITableViewScrollPositionBottom];
    }
    else
    {
        
        [self.showTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.count inSection:0]
                                        animated:YES
                                  scrollPosition:UITableViewScrollPositionTop];
    }
    
 
    self.count++;
    
   
    
}
-(void)viewWillAppear:(BOOL)animated
{
    //    //开启定时器
        [self.myTimer setFireDate:[NSDate distantPast]];
}

//页面消失,进入后台不显示该页面,关闭定时器
-(void)viewDidDisappear:(BOOL)animated
{
    //关闭定时器
    [self.myTimer setFireDate:[NSDate distantFuture]];
}


@end
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-02-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档