-
1.創(chuàng)建一個繼承自UITableViewCell的子類,比如IZGrouponCell
-
1.1 在IZGrouponCell.m中重寫initWithStyle:reuseIdentifier:方法
- 添加子控件
- 設(shè)置子控件的初始化屬性(比如文字顏色、字)
-
1.1 在IZGrouponCell.m中重寫initWithStyle:reuseIdentifier:方法
//在這個方法中添加所有的子控件
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
- **1.2 重寫`-layoutSubviews`方法**
>- 一定要調(diào)用`[super layoutSubviews]`
>- 在這個方法中計算和設(shè)置所有子控件的frame
```objc
//在這個方法中計算所有子控件的frame
-(void)layoutSubviews
{
[super layoutSubviews];
// ......
}
```
- **1.3 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設(shè)置模型數(shù)據(jù)到子控件**
>- 在IZGrouponCell.h文件中提供一個模型屬性,比如IZGroupon模型
@class IZGroupon;
@interface IZGrouponCell : UITableViewCell
/** 團購模型數(shù)據(jù) */
@property (nonatomic, strong) IZGroupon *groupon;
@end
>- 在IZGrouponCell.m中重寫模型屬性的set方法
-(void)setGroupon:(IZGroupon *)groupon
{
_groupon = groupon;
// .......
}
-
2.在控制器中
- 利用registerClass...方法注冊Cell類
[self.tableView registerClass:[IZGrouponCell class] forCellReuseIdentifier:ID];
- **給cell傳遞模型數(shù)據(jù)**
```objc
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 訪問緩存池
IZGrouponCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.groupon = self.groupon[indexPath.row];
return cell;
}