创建表单-定制视图单元
2011-09-19 13:02
375 查看
1.定制表单元-添加子视图
定义一个CellsViewController子视图
#import "CellsViewController"
@implementation CellsViewController
@synthesize computers;
-(void)viewDidload{
NSDictionary *row1=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook",@"Name",@"white",@"color",nil];
NSDictionary *row2=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook2",@"Name",@"white",@"color",nil];
NSDictionary *row3=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook3",@"Name",@"white",@"color",nil];
NSArray *array=[[NSArray alloc]initWithObjects:row1,row2,row3,nil];
self.computers=array;
[row1 release];
[row2 release];
[row3 release];
[array release];
}
#pragma mark table Data source methods
-(NSInteger)tableView:(UItableView *)tableView numberOfRowsInSecton:(NSiteger)section
{return [self.computers count];}
//自定义单元格 拥有两行的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CelltableIdentifier=@"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if(cell==nil){
CGRect cellFrame=CGRectMake(0,0,300,65);
cell=[[[UITableViewCell alloc]initWithFrame:cellFrame reseIdentifier:CellTableIdentifier]autorelease];
CGRect nameLableRect=CGRectMake(0,5,70,15);
UILabel *nameLabel=[[UILabel alloc]initWithFrame:nameLableRec];
nameLabel.textAlignment=UITextAlignmentright;
nameLabel.text=@"name";
nameLabel.font={UIFont boldSystemFontOfSize:12};
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect nameValueRect=CGRectMake(80,5,200,15);
UILabel *nameValue=[[UILabel alloc]initWithFrame:nameValueRect];
nameValue.tag=knameValuetag;
[cell.contentVie addSubview:nameValue];
[nameValue release];
//同理在创建一行COLOR
........
}
NSUInteger row=[indexPath row];
NSDictionary *rowData=[self.computers objectAtIndex:row];
UILabel *name=(UILabel *)[cell.contentView viewWithTag:kNameValuetag];
name.text=[rowData objectForKey:@"Name"];
UILabel *color=(UILabel *)[cell.contentView viewWithTag:kColorValuetag;
color.text=[rowData objectForKey:@"Color"];
return cell;
}
2.使用UITableViewCell的自定义子类
在子类的nib中添加UITableViewCell的子类到View中,构建需要的格式
添加方法:
-(id)initWithFram:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{if(self=[super initWithFrame:frame reuseIdentifier:reuseIdentifier])}
-(void)setSelected:(BOOL)selected animatied:(BOOL)animated
{[super setSelected:selected animated: ainmated];}
定义一个CellsViewController子视图
#import "CellsViewController"
@implementation CellsViewController
@synthesize computers;
-(void)viewDidload{
NSDictionary *row1=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook",@"Name",@"white",@"color",nil];
NSDictionary *row2=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook2",@"Name",@"white",@"color",nil];
NSDictionary *row3=[[NSDictonary alloc]initWithObjectsAndKeys:@"MacBook3",@"Name",@"white",@"color",nil];
NSArray *array=[[NSArray alloc]initWithObjects:row1,row2,row3,nil];
self.computers=array;
[row1 release];
[row2 release];
[row3 release];
[array release];
}
#pragma mark table Data source methods
-(NSInteger)tableView:(UItableView *)tableView numberOfRowsInSecton:(NSiteger)section
{return [self.computers count];}
//自定义单元格 拥有两行的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CelltableIdentifier=@"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if(cell==nil){
CGRect cellFrame=CGRectMake(0,0,300,65);
cell=[[[UITableViewCell alloc]initWithFrame:cellFrame reseIdentifier:CellTableIdentifier]autorelease];
CGRect nameLableRect=CGRectMake(0,5,70,15);
UILabel *nameLabel=[[UILabel alloc]initWithFrame:nameLableRec];
nameLabel.textAlignment=UITextAlignmentright;
nameLabel.text=@"name";
nameLabel.font={UIFont boldSystemFontOfSize:12};
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect nameValueRect=CGRectMake(80,5,200,15);
UILabel *nameValue=[[UILabel alloc]initWithFrame:nameValueRect];
nameValue.tag=knameValuetag;
[cell.contentVie addSubview:nameValue];
[nameValue release];
//同理在创建一行COLOR
........
}
NSUInteger row=[indexPath row];
NSDictionary *rowData=[self.computers objectAtIndex:row];
UILabel *name=(UILabel *)[cell.contentView viewWithTag:kNameValuetag];
name.text=[rowData objectForKey:@"Name"];
UILabel *color=(UILabel *)[cell.contentView viewWithTag:kColorValuetag;
color.text=[rowData objectForKey:@"Color"];
return cell;
}
2.使用UITableViewCell的自定义子类
在子类的nib中添加UITableViewCell的子类到View中,构建需要的格式
添加方法:
-(id)initWithFram:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{if(self=[super initWithFrame:frame reuseIdentifier:reuseIdentifier])}
-(void)setSelected:(BOOL)selected animatied:(BOOL)animated
{[super setSelected:selected animated: ainmated];}
相关文章推荐
- 代码实现UITableViewCell表视图单元定制
- 一步步学习微软InfoPath2010和SP2010--第五章节--添加逻辑和规则到表单(6)--使用规则创建多视图样式向导表单
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- 一步步学习微软InfoPath2010和SP2010--第五章节--添加逻辑和规则到表单(6)--使用规则创建多视图样式向导表单
- 一步步学习微软InfoPath2010和SP2010--第十一章节--创建批准流程(3)--表单视图
- 一步步学习微软InfoPath2010和SP2010--第十一章节--创建批准流程(3)--表单视图
- 创建定制视图(1)
- 【转载】创建定制ASP.NET MVC视图引擎
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- IOS5基础之十一-----定制表视图单元
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- 【转】Ruby on Rails,创建最简单的视图/控制器单元Hello World
- 代码实现UITableViewCell表视图单元定制
- 代码实现UITableViewCell表视图单元定制
- Myeclipse工作空间Servers创建视图失败解决办法
- Oracle创建视图的语法 <转>
- visualnet管网管理系统的模板创建与属性定制
- 用Kotlin开发Android应用(IV):定制视图和Android扩展
- 创建tabBarControllers视图控制器的方法
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(4)--控件属性功能区