tableView - 自定义等高cell-xib方式

xib自定义cell

  • 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
  • 2.创建一个xib文件(文件名建议跟cell的类名一样),比如WQDealCell.xib
    • 拖拽一个UITableViewCell出来
    • 修改cell的class为XMGDealCell
    • 设置cell的重用标识
    • 往cell中添加需要用到的子控件
  • 3.在控制器中
    • 利用registerNib…方法注册xib文件
    • 利用重用标识找到cell(如果没有注册xib文件,就需要手动去加载xib文件)
    • 给cell传递模型数据
  • 4.在WQDealCell中

    • 将xib中的子控件连线到类扩展中
    • 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
    • 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
  • 当在控制器中提前注册后,cell在dequeueReusableCellWithIdentifier缓冲池找不到后,系统会自动创建带有该标识符的cell,后面cell始终不为nil

  • 当不注册的时候,需要手动加载xib文件

    “`objc
    // 提前注册xib,一般在初始化方法中
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WQTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ID];

// 手动加载xib文件,在创建cell方法中
// 当使用注册方法优化cell创建的时候,cell永远不为nil,不进入该方法
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

“`

  • 代码:
#import <UIKit/UIKit.h>
@class WQdealsData;
@interface WQTableViewCell : UITableViewCell
/**cell的数据模型*/
@property (nonatomic, strong)  WQdealsData *dealData;
+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView;
@end

#import "WQTableViewCell.h"
#import "WQdealsData.h"

@interface WQTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
@implementation WQTableViewCell

+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"deals";
    WQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    // 当使用注册方法优化cell创建的时候,cell永远不为nil,不进入该方法
    if (cell == nil) {

    }
    return cell;
}

- (void)setDealData:(WQdealsData *)dealData
{
    _dealData = dealData;
    self.iconImageView.image = [UIImage imageNamed:self.dealData.icon];
    self.titleLabel.text = self.dealData.title;
    self.priceLabel.text = [NSString stringWithFormat:@"%@¥", self.dealData.price];
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", self.dealData.buyCount];

}
@end

#import "WQDealsTableViewController.h"
#import "WQdealsData.h"
#import "WQTableViewCell.h"
@interface WQDealsTableViewController ()
/**数据模型*/
@property (nonatomic, strong) NSArray *dealsData;

@end

@implementation WQDealsTableViewController
NSString *ID = @"deals";
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WQTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ID];
}
/** 懒加载*/
- (NSArray *)dealsData
{
    NSArray *dataArrayFromPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil]];
    NSMutableArray *dataArray = [NSMutableArray array];
    [dataArrayFromPlist enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        WQdealsData *dealsdata = [WQdealsData dealsWithDict:obj];
        [dataArray addObject:dealsdata];
    }];
    _dealsData = dataArray;
    return _dealsData;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dealsData.count;
}
/** 创建cell*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建自定义的tableViewCell
    WQTableViewCell *cell = [WQTableViewCell tableViewCellWithTableView:tableView];
    // 向自定的tableViewCell中传入数据
    cell.dealData = self.dealsData[indexPath.row];
    return cell;
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值