您的位置:首页 > 其它

创建表单-定制视图单元

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];}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  table