横向TableVIew的实现

来源:互联网 发布:手表定位软件 编辑:程序博客网 时间:2024/05/21 15:39
方法一:设置tableview属性:
           _tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);

           设置cell属性
           cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2); 

方法二:横向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:framestyle: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

{

   staticNSString *identify =@"cell";

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier: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:indexPathatScrollPosition: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:rowinSection:0];

    

    //滚动到指定单元格

    [selfscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionTopanimated:YES];

    

    //记下选中的单元格索引

   self.selectedIndexPath = indexPath;

    

}

@end



0 0
原创粉丝点击