用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 {
//必須對(duì)屬性進(jìn)行賦值
_groupnModel= groupnModel;
//對(duì)內(nèi)部的子控件進(jìn)行賦值
_shopImageView.image= [UIImageimageNamed:groupnModel.icon];
_titleLabel.text= groupnModel.title;
_priceLabel.text= [NSStringstringWithFormat:@"$%@", groupnModel.price];
_buyCountLabel.text= [NSStringstringWithFormat:@"已購(gòu)買%@", groupnModel.buyCount];
}
@end
注意:記得從XIB加載的時(shí)候 系統(tǒng)是調(diào)用? - (void)awakeFromNib 方法
主viewController里面關(guān)鍵代碼
//每一行要顯示的內(nèi)容
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// 1.先定義重用標(biāo)識(shí)符
staticNSString*identifier =@"groupn";
// 2.根據(jù)重用標(biāo)識(shí)符到緩存池中去找
GroupnCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
// 3.判斷查找到的cell是否為nil
if(nil== cell) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
#warning使用xib的時(shí)候,一定不要忘記在xib中設(shè)置重用標(biāo)識(shí)符
cell = [[NSBundlemainBundle]loadNibNamed:@"GroupnCell"owner:niloptions:nil].lastObject;
NSLog(@"----");
}
//取出indexPath.row對(duì)應(yīng)的模型
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;
}