UITableView横用

大多数情况下,UITableView的使用时其默认方向,即竖向上下滑动,但有时候需要左右滑动的情况,虽然UIScrollView,UIPageViewController等也可以,但将UITableview横用似乎也是一个不错的选择:

下面直接看代码,比较简单

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * _tableViewDataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    _tableViewDataArray = [[NSMutableArray alloc] initWithArray:@[@"第一页", @"第二页", @"第三页", @"第四页", @"第五页"]];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, height, width) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.center = self.view.center;
    _tableView.pagingEnabled = YES;
    //旋转整个tableView
    _tableView.transform = CGAffineTransformMakeRotation(-M_PI/2);
    [self.view addSubview:_tableView];
}


#pragma mark ---
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableViewDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        //此处再将每个cell旋转回来,否则就要侧着头看了
        cell.transform = CGAffineTransformMakeRotation(M_PI/2);
    }
    //设置不同背景色,易区分
    cell.contentView.backgroundColor = @[[UIColor whiteColor],[UIColor purpleColor],[UIColor brownColor], [UIColor orangeColor], [UIColor blueColor]][indexPath.row%5];
    cell.textLabel.text = _tableViewDataArray[indexPath.row];
    return cell;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[UIScreen mainScreen] bounds].size.width;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果:


框架已好,至于怎么定制cell,就看你自己的了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值