创建表单-定制视图单元

来源:互联网 发布:淘宝网购物女装卫衣 编辑:程序博客网 时间:2024/04/29 07:48


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

 

原创粉丝点击