美文网首页
等高Cell

等高Cell

作者: FredYJH | 来源:发表于2016-12-10 22:23 被阅读8次

一、自定义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很像

相关文章

  • 等高Cell

    一、自定义Cell1、等高cell 代码 很古老的方法: 利用autoLayout xib加载xib要通过手动加载...

  • 不等高cell自定义的思路精髓

    ## 精华界面不等高cell的搭建 1 . 精华界面搭建,tableview展示数据; 不等高cell(复杂界面)...

  • 基础 (十六) : 等高cell

    等高Cell-frame: 等高Cell-frame 知识回顾 拖入plist文件以及图片 1.(tableVie...

  • 自定义不等高cell

    自定义不等高cell 自定义不等高cell(纯代码) 给模型增加frame数据 所有子控件的frame cell...

  • 巧用AutoLayout自动计算cell高度

    cell的高度 cell的高度可以简单分为等高和不等高两大类。等高的情况不用多说,直接设置表视图的cellHeig...

  • cell的等高与不等高

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • 自定义等高的cell

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • day10-UITableView-自定不等高的cell-sto

    自定不等高的cell-storyboard版 01-无配图自定不等高的cell-storyboard版 02-有配...

  • 不等高Cell

    计算字符串一行的高度 计算字符串多行的高度

  • 不等高cell

    纯代码创建 frame 一.给模型增加frame数据 所有子控件的frame cell的高度 二.重写模型cell...

网友评论

      本文标题:等高Cell

      本文链接:https://www.haomeiwen.com/subject/rvjumttx.html