大多数情况下,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,就看你自己的了