等高Cell

一、自定义Cell
1、等高cell

  • 代码

很古老的方法:

#import "YJHTableViewCell.h"

@interface YJHTableViewCell()
@property (nonatomic, weak) UIImageView *iconImageView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *priceLabel;
@property (nonatomic, weak) UILabel *buyCountLabel;

@end

@implementation YJHTableViewCell

// 这个方法添加所有控件,调用init、initWithFrame都会通过该方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIImageView *iconImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconImageView];
        self.iconImageView = iconImageView;
        self.iconImageView.backgroundColor = [UIColor greenColor];

        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        self.titleLabel.backgroundColor = [UIColor redColor];

        UILabel *priceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:priceLabel];
        self.priceLabel = priceLabel;
        self.priceLabel.backgroundColor = [UIColor yellowColor];

        UILabel *buyCountLabel = [[UILabel alloc] init];
        [self.contentView addSubview:buyCountLabel];
        self.buyCountLabel = buyCountLabel;
        self.buyCountLabel.backgroundColor = [UIColor blueColor];

    }
    return self;
}
// 设置所有子控件的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat space = 10;
    CGFloat contentH = self.contentView.frame.size.height;
    CGFloat contentW = self.contentView.frame.size.width;

    
    CGFloat iconX = space;
    CGFloat iconY = space;
    CGFloat iconW = 80;
    CGFloat iconH = contentH - 2 * space;
    self.iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    
    CGFloat titleX = CGRectGetMaxX(self.iconImageView.frame)+space;
    CGFloat titleY = iconY;
    CGFloat titleW = contentW - titleX - space;
    CGFloat titleH = 20;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    CGFloat priceX = iconX + iconW + space;
    CGFloat priceY = CGRectGetMaxY(self.iconImageView.frame) - 20;
    CGFloat priceW = 100;
    CGFloat priceH = 20;
    self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH);
    
    CGFloat buyCountX = contentW - space - 50;
    CGFloat buyCountY = CGRectGetMaxY(self.iconImageView.frame) - 20;
    CGFloat buyCountW = 50;
    CGFloat buyCountH = 20;
    self.buyCountLabel.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);
}
@end

利用autoLayout


#import "YJHTableViewCell.h"
#import "Masonry.h"
@interface YJHTableViewCell()
@property (nonatomic, weak) UIImageView *iconImageView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *priceLabel;
@property (nonatomic, weak) UILabel *buyCountLabel;

@end

@implementation YJHTableViewCell

// 这个方法添加所有控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGFloat space = 10;
        UIImageView *iconImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconImageView];
        self.iconImageView = iconImageView;
        self.iconImageView.backgroundColor = [UIColor greenColor];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).offset(space);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-space);
            make.left.mas_equalTo(self.contentView.mas_left).offset(space);
            make.width.mas_equalTo(80);
        }];

        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        self.titleLabel.backgroundColor = [UIColor redColor];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(iconImageView.mas_top);
            make.left.mas_equalTo(iconImageView.mas_right).offset(space);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-space);
            make.height.mas_equalTo(20);

        }];

        UILabel *priceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:priceLabel];
        self.priceLabel = priceLabel;
        self.priceLabel.backgroundColor = [UIColor yellowColor];
        
        [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(iconImageView.mas_right).offset(space);
            make.bottom.mas_equalTo(iconImageView.mas_bottom);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(20);

        }];

        UILabel *buyCountLabel = [[UILabel alloc] init];
        [self.contentView addSubview:buyCountLabel];
        self.buyCountLabel = buyCountLabel;
        self.buyCountLabel.backgroundColor = [UIColor blueColor];
        [buyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(iconImageView.mas_bottom);
            make.right.mas_equalTo(titleLabel.mas_right);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(20);

        }];

    }
    return self;
}

  • xib
    加载xib要通过手动加载:[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
    下面是数据和界面的关系:
屏幕快照 2016-12-11 上午12.01.57.png
  • storyboard
    和xib很像
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容