<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">自定义单元格有两种方法:</span>
1、通过加载xib的方式(如果单元格的样式都一样的话,这种方法很便捷)注:这种方式只能用于单元格样式一样的情况
2、全部手写代码
通过加载xib的方式自定义单元格:
设置重用ID:
CZTableViewCell.h文件:
#import <UIKit/UIKit.h>
@class CZGoods;
@interface CZTableViewCell : UITableViewCell
@property (nonatomic, copy) CZGoods *model;
+ (CZTableViewCell *)tableViewCellTableView:(UITableView *)tableView;
@end
CZTableViewCell.m文件:
#import "CZTableViewCell.h"
#import "CZGoods.h"
@interface CZTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *textLable;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@end
@implementation CZTableViewCell
// 重写model的set方法加载数据
- (void)setModel:(CZGoods *)model
{
_model = model;
self.img.image = [UIImage imageNamed:self.model.icon];
self.titleLabel.text = self.model.title;
self.textLable.text = [NSString stringWithFormat:@"¥ %@", self.model.price];
self.numLabel.text = [NSString stringWithFormat:@"%@人已购买", self.model.buyCount];
}
+ (CZTableViewCell *)tableViewCellTableView:(UITableView *)tableView
{
// 设置重用id
NSString *ID = @"good";
CZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"UITableViewCell" owner:nil options:nil] lastObject];
}
return cell;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end