新建一個(gè)繼承自UITableViewCell
的子類(lèi),比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
在XMGTgCell.m文件中
- 重寫(xiě)
-initWithStyle:reuseIdentifier:
方法
- 在這個(gè)方法中添加所有的子控件
- 給子控件做一些初始化設(shè)置(設(shè)置字體命迈、文字顏色等)
添加子控件的完整約束
/**
* 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
在XMGTgCell.h文件中提供一個(gè)模型屬性声诸,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
在XMGTgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg
{
_tg = tg;
// .......
}
在控制器中
[self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 訪問(wèn)緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者