tableView - 自定义等高cell(frame和masonry方式)

来源:互联网 发布:入骨相思知不知 编辑:程序博客网 时间:2024/06/04 16:35

代码自定义cell方法

  • 代码自定义cell(使用frame)
    • 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
      • 在initWithStyle:reuseIdentifier:方法中
        • 添加子控件
        • 设置子控件的初始化属性(比如文字颜色、字体)
      • 在layoutSubviews方法中设置子控件的frame
      • 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
    • 2.在控制器中
      • 利用registerClass…方法注册WQDealCell类
      • 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
      • 给cell传递模型数据
      • 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
#import <UIKit/UIKit.h>@class WQdealsData;@interface WQTableViewCell : UITableViewCell/**数据模型接口*/@property (nonatomic, strong) WQdealsData *dealData;+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView;@end#import "WQTableViewCell.h"#import "WQdealsData.h"@interface WQTableViewCell ()/**图片*/@property (nonatomic, strong) UIImageView *iconImageView;/**标题*/@property (nonatomic, strong) UILabel *titleLabel;/**价格*/@property (nonatomic, strong) UILabel *priceLabel;/**购买人数*/@property (nonatomic, strong) UILabel *buyCountLabel;@end@implementation WQTableViewCell+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView{    static NSString *ID = @"deals";    WQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    // 当使用注册方法优化cell创建的时候,cell永远不为nil,不进入该方法    if (cell == nil) {        cell = [[WQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];    }    return cell;}// 1.在initWithStyle:reuseIdentifier:方法中添加子控件,背景色用于测试添加位置是否正确- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {        // 添加imageView        self.iconImageView = [[UIImageView alloc]init];        _iconImageView.backgroundColor = [UIColor redColor];        [self.contentView addSubview:self.iconImageView];        // 添加title        self.titleLabel = [[UILabel alloc]init];        //self.titleLabel.backgroundColor = [UIColor blueColor];        //self.titleLabel.textColor = [UIColor redColor];        [self.contentView addSubview:self.titleLabel];        // 添加price        self.priceLabel = [[UILabel alloc]init];        //self.priceLabel.backgroundColor = [UIColor orangeColor];        self.priceLabel.textColor = [UIColor redColor];        [self.contentView addSubview:self.priceLabel];        // 添加buyCount        self.buyCountLabel = [[UILabel alloc]init];        //self.buyCountLabel.backgroundColor = [UIColor grayColor];        self.buyCountLabel.textColor = [UIColor redColor];        self.buyCountLabel.textAlignment = NSTextAlignmentRight;        [self.contentView addSubview:self.buyCountLabel];    }    return self;}// 3,在数据模型接口setter方法中,给控件赋值- (void)setDealData:(WQdealsData *)dealData{    _dealData = dealData;    self.iconImageView.image = [UIImage imageNamed:_dealData.icon];    self.titleLabel.text = _dealData.title;    self.priceLabel.text = [NSString stringWithFormat:@"%@¥", _dealData.price];    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", _dealData.buyCount];}// 2.在layoutSubviews方法中设置子控件的frame- (void)layoutSubviews{    [super layoutSubviews];    CGFloat contentW = self.contentView.frame.size.width;    CGFloat contentH = self.contentView.frame.size.height;    CGFloat margin = 10;    // imageView    CGFloat iconX = margin;    CGFloat iconY = margin;    CGFloat iconW = 100;    CGFloat iconH = contentH - margin * 2;    self.iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);    // title    CGFloat titleX = CGRectGetMaxX(self.iconImageView.frame) + margin;    CGFloat titleY = margin;    CGFloat titleW = contentW - titleX - margin;    CGFloat titleH = 20;    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);    // price    CGFloat priceX = titleX;    CGFloat priceY = margin * 2 + titleH;    CGFloat priceW = 80;    CGFloat priceH = contentH - margin * 3 - titleH;    self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH);    // buyCount    CGFloat buyCountX = CGRectGetMaxX(self.priceLabel.frame);    CGFloat buyCountY = priceY;    CGFloat buyCountW = contentW - buyCountX - margin;    CGFloat buyCountH = priceH;    self.buyCountLabel.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);}// "WQTableViewController.h" 控制器中创建cell,控制器不需要知道cell具体实现,只创建传入数据即可
  • 代码自定义cell(使用autolayout)
    • 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
      • 在initWithStyle:reuseIdentifier:方法中
        • 添加子控件
        • 添加子控件的约束(建议使用Masonry
        • 设置子控件的初始化属性(比如文字颜色、字体)
      • 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
    • 2.在控制器中
      • 利用registerClass…方法注册WQDealCell类
      • 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
      • 给cell传递模型数据
      • 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
//两个宏定义一定要在头文件之前,因为masonry实现文件里需要用到//define this constant if you want to use Masonry without the 'mas_' prefix#define MAS_SHORTHAND//define this constant if you want to enable auto-boxing for default syntax#define MAS_SHORTHAND_GLOBALS#import "Masonry.h"//创建完子控件后,直接添加约束// 1.在initWithStyle:reuseIdentifier:方法中添加子控件,背景色用于测试添加位置是否正确- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {        CGFloat margin = 10;        // 添加imageView        self.iconImageView = [[UIImageView alloc]init];        [self.contentView addSubview:self.iconImageView];        // 添加约束        [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {            make.top.left.offset(margin);            make.bottom.offset(- margin);            make.width.equalTo(100);        }];        // 添加title        self.titleLabel = [[UILabel alloc]init];        [self.contentView addSubview:self.titleLabel];        // 添加约束        [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(self.iconImageView.right).offset(margin);            make.top.offset(margin);            make.right.offset(- margin);        }];        // 添加price        self.priceLabel = [[UILabel alloc]init];        self.priceLabel.textColor = [UIColor redColor];        [self.contentView addSubview:self.priceLabel];        // 添加约束        [self.priceLabel makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(self.titleLabel.left);            make.width.equalTo(80);            make.bottom.offset(-margin);        }];        // 添加buyCount        self.buyCountLabel = [[UILabel alloc]init];        self.buyCountLabel.textColor = [UIColor redColor];        self.buyCountLabel.textAlignment = NSTextAlignmentRight;        [self.contentView addSubview:self.buyCountLabel];        // 添加约束        [self.buyCountLabel makeConstraints:^(MASConstraintMaker *make) {            make.left.equalTo(self.priceLabel.right).offset(margin);            make.right.equalTo(- margin);            make.bottom.equalTo(self.priceLabel.bottom);        }];    }    return self;}
0 0
原创粉丝点击