模型相关联起来。我们需要在cell中往外暴露一个方法,去接收模型,同样的原因,我们在cell的.h文件中声明这个方法:
- (void)customWithMyModel:(MyModel *)bm;
然后在cell的.m文件中来实现这个方法,参数类型就是我们自定义的这个模型类
- (void)customWithMyModel:(MyModel *)bm{
_iconView.image = bm.icon;
_nameLabel.text = bm.nameStr;
_priceLabel.text = bm.priceStr;
_detailLabel.text = bm.detailStr;
}
到这里为止,我们的自定义部分基本就结束了,接下来就是在Controller里面去使用我们自定义的Cell了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *idt= @"idt";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:idt];
if (!cell) {
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:idt];
}
MyModel *mm= [_dataArrobjectAtIndex:indexPath.row-1];
//将数据模型传给cell,让cell设置图片及文字
[cell customWithMyModel:mm];
return cell;
}
这样就能创建一个我们自定义的,使用MVC思想架构的cell了,数据怎么接?那就是另外一篇笔记了哈哈。
|