新建一个继承自UITableViewCell的子类,比如JCGroupPurchaseCell
@interface JCGroupPurchaseCell : UITableViewCell
@end
在JCGroupPurchaseCell.m文件中
重写-initWithStyle:reuseIdentifier:方法
在这个方法中添加所有的子控件给子控件做一些初始化设置(设置字体、文字颜色等)添加子控件的完整约束
/**
* 在这个方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [
super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}
在JCGroupPurchaseCell.h文件中提供一个模型属性,比如JCGroupPurchase模型
@class JCGroupPurchase;
@interface JCGroupPurchaseCell : UITableViewCell
/** 团购模型数据 */
@property (nonatomic, strong) JCGroupPurchase *groupPurchase;
@end
在JCGroupPurchaseCell.m中重写模型属性的set方法
在set方法中给子控件设置模型数据
- (void)setGroupPurchase:(JCGroupPurchase *)groupPurchase
{
_groupPurchase = groupPurchase;
// .......
}
在控制器中
注册cell的类型
[
self.tableView
registerClass:[
JCGroupPurchaseCell class] forCellReuseIdentifier:ID];
给cell传递模型数据
- (
UITableViewCell *)tableView:(
UITableView *)tableView cellForRowAtIndexPath:(
NSIndexPath *)indexPath
{
JCGroupPurchaseCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
cell
.groupPurchase =
self.groupPurchases[indexPath
.row];
return cell;
}
转载请注明原文地址: https://ju.6miu.com/read-663093.html