用XIB 自定Cell
@interfaceGroupnCell()
@property(weak,nonatomic)IBOutletUIImageView*shopImageView;
@property(weak,nonatomic)IBOutletUILabel*titleLabel;
@property(weak,nonatomic)IBOutletUILabel*priceLabel;
@property(weak,nonatomic)IBOutletUILabel*buyCountLabel;
@end
@implementationGroupnCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setGroupnModel:(GroupnModel*)groupnModel {
//必须对属性进行赋值
_groupnModel= groupnModel;
//对内部的子控件进行赋值
_shopImageView.image= [UIImageimageNamed:groupnModel.icon];
_titleLabel.text= groupnModel.title;
_priceLabel.text= [NSStringstringWithFormat:@"$%@", groupnModel.price];
_buyCountLabel.text= [NSStringstringWithFormat:@"已购买%@", groupnModel.buyCount];
}
@end
注意:记得从XIB加载的时候 系统是调用 - (void)awakeFromNib 方法
主viewController里面关键代码
//每一行要显示的内容
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// 1.先定义重用标识符
staticNSString*identifier =@"groupn";
// 2.根据重用标识符到缓存池中去找
GroupnCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
// 3.判断查找到的cell是否为nil
if(nil== cell) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
#warning使用xib的时候,一定不要忘记在xib中设置重用标识符
cell = [[NSBundlemainBundle]loadNibNamed:@"GroupnCell"owner:niloptions:nil].lastObject;
NSLog(@"----");
}
//取出indexPath.row对应的模型
GroupnModel*model =self.dataArray[indexPath.row];
////赋值
//cell.imageView.image = [UIImage imageNamed:model.icon];
//
//cell.textLabel.text = model.title;
//
//cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", model.price];
returncell;
}