自定义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”];
}
===========================================