自定义cell
思路
1.新建CustomTableViewCell 继承自UITableViewCell
2.写方法
3.填充数据
4. 重写row方法
5.加入数据,写在return cell;前面
1.新建CustomTableViewCell 继承自UITableViewCell
@interface CustomTableViewCell : UITableViewCell
2.写方法
//该方法可复制
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self createUI];
}
return self;
}
- (void)createUI
{
//在此写控件,加入到self.contentView上
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
[self.contentView addSubview:label];
self.label = label;
}
3.填充数据
/**
* 填充数据
*/
- (void)fillDataWithModel:(CellModel *)model;
- (void)fillDataWithModel:(CellModel *)model
{
self.label.text = model.text;
}
4. 重写- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"iden"];
if (!cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"iden"];
}
return cell;
}
5.加入数据,写在return cell;前面
CellModel * model = self.dataList[indexPath.row];
[cell fillDataWithModel:model];