方法二:横向UITableView已经有开源实现了 ,EasyTableView,https://github.com/alekseyn/EasyTableView
#import <UIKit/UIKit.h>
@interface BasePictureTableView :UITableView<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSArray *data;
//选中单元格IndexPath
@property(nonatomic,retain)NSIndexPath *selectedIndexPath;
@end
#import "BasePictureTableView.h"
@implementation BasePictureTableView
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [superinitWithFrame:frame style:style];
if (self) {
[self_initViews:frame];
}
return self;
}
- (void)awakeFromNib
{
[superawakeFromNib];
[self_initViews:self.frame];
}
- (void)_initViews:(CGRect)frame
{
//1.逆时针旋转90度
self.transform =CGAffineTransformMakeRotation(-M_PI_2);
//2.旋转之后宽高互换了,所以重新设置frame
self.frame = frame;
self.dataSource =self;
self.delegate =self;
//3.隐藏滚动条
self.showsVerticalScrollIndicator =NO;
//4.设置减速的方式,UIScrollViewDecelerationRateFast快速减速
self.decelerationRate =UIScrollViewDecelerationRateFast;
//5.背景设置为透明
self.backgroundColor = [UIColorclearColor];
//6.隐藏分割线
self.separatorStyle =UITableViewCellSeparatorStyleNone;
}
#pragma mark - override layoutSubViews
#warning mark 第九天修改
- (void)setRowHeight:(CGFloat)rowHeight
{
super.rowHeight = rowHeight;
CGFloat edge = (self.width-self.rowHeight)/2;
//设置滚动的区域
self.contentInset =UIEdgeInsetsMake(edge, 0, edge, 0);
}
#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identify =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identify] autorelease];
cell.contentView.backgroundColor = [UIColorgrayColor];
//将单元格视图顺时针旋转90度
cell.contentView.transform =CGAffineTransformMakeRotation(M_PI_2);
}
return cell;
}
//- (void)layoutSubviews
//{
// CGFloat edge = (self.width - self.rowHeight)/2;
// self.contentInset = UIEdgeInsetsMake(edge, 0, edge, 0);
// [super layoutSubviews];
//}
//点击单元格调用的协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//将单元格滚动到中间
[selfscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:YES];
//记下选中的单元格索引
self.selectedIndexPath = indexPath;
}
//停止拖拽调用,(手指离开滚动视图时调用)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//手指离开视图时,滚动视图是禁止状态在调用scrollCellToCenter
if (!decelerate) {
[selfscrollCellToCenter];
}
}
//已经减速停止后调用的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[selfscrollCellToCenter];
}
//将单元格滚动到中间位置
- (void)scrollCellToCenter
{
//计算居中单元格的行索引
// CGFloat edge = (self.width - self.rowHeight)/2;
CGFloat edge =self.contentInset.top;
float y =self.contentOffset.y + edge+self.rowHeight/2;
int row = y / self.rowHeight;
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:row inSection:0];
//滚动到指定单元格
[selfscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:YES];
//记下选中的单元格索引
self.selectedIndexPath = indexPath;
}
@end