新建一个继承自UITableViewCell的子类,比如HZKTgCell
@interface XMGTgCell : UITableViewCell
@end
在HZKTgcell.m文件
- 重写
-initWithStyle:reuseIdentifier:方法
- 在这个方法中添加所有的子控件
- 给子控件做一些初始化设置(设置字体、文字颜色等)
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}
- 重写
-layoutSubviews方法
- 一定要调用
[super layoutSubviews] - 在这个方法中计算和设置所有子控件的frame
- (void)layoutSubviews
{
[super layoutSubviews];
}
在HZKTgCell.h文件中提供一个模型属性,比如HZKTg模型
@class HZKTg;
@interface HZKTgCell : UITableViewCell
@property (nonatomic, strong) HZKTg *tg;
@end
在XMGTgCell.m中重写模型属性的set方法
- (void)setTg:(XMGTg *)tg
{
_tg = tg;
}
在控制器中
[self.tableView registerClass:[HZKTgCell class] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HZKTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell.tg = self.tgs[indexPath.row];
return cell;
}