首先附上效果图
进行描述一下:效果就是类似于是一个竖直方向的滚动视图 并且方向是从下往上 并且能够一直这样循环下去。
代码“
//
// 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