30.自定义UITableViewCell第一章

来源:互联网 发布:淘宝o2o平台 编辑:程序博客网 时间:2024/05/01 01:35

自定义cell(只要是自定义控件 都这个步骤)

1.创建TableViewCell子类

2.重写初始化方法

3.把要添加的控件 添加cell的显示内容区域 contentView上面

4.把系统的cell 替换成 自定义cell 完成

步骤1.创建TableViewCell子类

@interface MyTableViewCell : UITableViewCell

步骤2.重写初始化方法

1. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

[self addSubview];

}

return self;

}

步骤3:把要添加的控件 添加cell的显示内容区域 contentView上面

- (void)addSubview

{

self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kMargin,kMargin,kImageWidth,kImageHeight)];

self.imageV.backgroundColor = [UIColor yellowColor];

[self.contentView addSubview:self.imageV];

[self.imageV release];

self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.imageV.right + kMargin, self.imageV.top, kScreenWidth - 3 * kMargin - self.imageV.right, self.imageV.height/3 - kLabelMargin)];

self.nameLabel.backgroundColor = [UIColor redColor];

[self.contentView addSubview:self.nameLabel];

[self.nameLabel release];

self.phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.left, self.nameLabel.bottom + kLabelMargin, self.nameLabel.width, self.nameLabel.height)];

self.phoneLabel.backgroundColor = [UIColor redColor];

[self.contentView addSubview:self.phoneLabel];

[self.phoneLabel release];

self.ganderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneLabel.left, self.phoneLabel.bottom + kLabelMargin, self.phoneLabel.width, self.phoneLabel.height)];

self.ganderLabel.backgroundColor = [UIColor redColor];

[self.contentView addSubview:self.ganderLabel];

[self.ganderLabel release];

}

为了别人方便重写model的setter方法 使赋值model的同时 也对控件进行赋值

- (void)setModel:(CellModel *)model

{

if (_model != model) {

[_model release];

_model = [model retain];

}

self.nameLabel.text = model.name;

self.phoneLabel.text = model.phoneNumber;

self.ganderLabel.text = model.gender;

self.imageV.image = [UIImage imageNamed:@”nvshen”];

}

===========================================

复合cell

数据加载

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

根据model值进行判断 显示不同的cell

CellModel *model = self.dataArray[indexPath.row];

if ([model.gender isEqualToString:@”男”]) {

创建男的

static NSString *identifier = @”Man=Cell”;

ManTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[[ManTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

}

再给model赋值的同时 咱们希望 也给cell上的控件完成赋值

cell.model = model;

return cell;

}else{

女的

static NSString *identifier = @”GirlCell”;

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

}

再给model赋值的同时 咱们希望 也给cell上的控件完成赋值

cell.model = model;

return cell;

}

0 0