您的位置:首页 > 移动开发 > IOS开发

iOS 竖向tableView上添加横向tableView

2013-07-09 14:03 323 查看
要点一:使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: