自定义单元格

<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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值