iOS 竖向tableView上添加横向tableView

要点一:使用CGAffineTransform来270度旋转UITableView

要点二: 创建一个UITableViewCell的子类,在该类中将横向tableView添加到cell中

具体实现见代码:

     效果如下图:

 

Cell.h文件

#import <UIKit/UIKit.h>

@interface Cell : UITableViewCell<UITableViewDataSource,UITableViewDelegate>
{
    CGAffineTransform transform;
}
@end

Cell.m文件

#import "Cell.h"

@implementation Cell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UITableView * tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 60, 320) style:UITableViewStylePlain];
        tableview.dataSource=self;
        tableview.delegate=self;
        transform=CGAffineTransformMakeRotation(M_PI/2*3);
        tableview.transform=transform;
        tableview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        tableview.center=CGPointMake(160, 30);
        tableview.backgroundColor=[UIColor lightGrayColor];
        [self addSubview:tableview];
       
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    CGAffineTransform tran=CGAffineTransformMakeRotation(M_PI/2);
//    
//    cell.transform=tran;
//}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier=@"cell";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
        label.text=@"11111";
        CGAffineTransform tran=CGAffineTransformMakeRotation(M_PI/2);
        
        cell.transform=tran;
        label.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
        
            }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

ViewController.h文件

#import <UIKit/UIKit.h>
#import "Cell.h"
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@end

ViewController.m文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView * table1=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    table1.delegate=self;
    table1.dataSource=self;
    [self.view addSubview:table1];
    [table1 release];
	// Do any additional setup after loading the view.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier=@"cell";
    Cell * cell1=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell1==nil) {
        cell1=[[Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   return cell1;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值