[自定义不等高的cell]-纯代码Autolayout方式iOS8之后

// ViewController.h
#import 

@interface ViewController : UITableViewController


@end

// ViewController.m
#import "ViewController.h"
#import "XMGStatusCell.h"
#import "XMGStatus.h"
#import "MJExtension.h"

@interface ViewController ()
/** 所有的微博模型*/
@property (nonatomic ,strong) NSArray *statuses;
@end

@implementation ViewController


- (NSArray *)statuses
{
    if (!_statuses) {
        _statuses = [XMGStatus mj_objectArrayWithFilename:@"statuses.plist"];
    }
    return _statuses;
}

NSString *ID = @"status";
- (void)viewDidLoad {
    [super viewDidLoad];

    // 注册方法
    [self.tableView registerClass:[XMGStatusCell class] forCellReuseIdentifier:ID];

    // 大概行高,iOS8之后的技术
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;

}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 传递模型数据
    cell.status = self.statuses[indexPath.row];
    return cell;
}

@end

// 模型数据
// XMGStatus.h
#import 

#define XMGTextFont [UIFont systemFontOfSize:14]
#define XMGNameFont [UIFont systemFontOfSize:17]
@interface XMGStatus : NSObject

/** 图像*/
@property (nonatomic ,copy) NSString *icon;

/** 昵称*/
@property (nonatomic ,copy) NSString *name;

/** 内容(正文)*/
@property (nonatomic ,copy) NSString *text;

/** vip*/
@property (nonatomic ,assign ,getter=isVip) BOOL vip;

/** 配图*/
@property (nonatomic ,copy) NSString *picture;


@end


// XMGStatus.m
#import "XMGStatus.h"

@implementation XMGStatus

@end

// XMGStatusCell.h
#import 

@class XMGStatus;
@interface XMGStatusCell : UITableViewCell

/** 微博模型*/
@property (nonatomic ,strong) XMGStatus *status;

@end

// XMGStatusCell.m
#import "XMGStatusCell.h"
#import "XMGStatus.h"

//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"

@interface XMGStatusCell ()
/** 图像*/
@property (nonatomic ,weak)UIImageView *iconImageView;

/** 昵称*/
@property (nonatomic ,weak)UILabel *nameLabel;

/** vip*/
@property (nonatomic ,weak)UIImageView *vipImageView;

/** 正文*/
@property (nonatomic ,weak)UILabel *text_Label;

/** 配图*/
@property (nonatomic ,weak)UIImageView *pictureImageView;
@end

@implementation XMGStatusCell

CGFloat space = 10;

// 把所有有可能显示的子控件都先添加进去
- (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;
        // 添加约束
        [iconImageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.left).offset(space);
            make.top.equalTo(self.contentView.top).offset(space);
            make.width.equalTo(40);
            make.height.equalTo(40);
        }];

        // 昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = XMGNameFont;
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        // 添加约束
        [nameLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.right).offset(space);
            make.top.equalTo(iconImageView.top);
            make.width.lessThanOrEqualTo(120);
        }];

        // vip
        UIImageView *vipImageView = [[UIImageView alloc] init];
        vipImageView.image = [UIImage imageNamed:@"vip"];
        vipImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:vipImageView];
        self.vipImageView = vipImageView;
        // 添加约束
        [vipImageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(nameLabel.right).offset(space);
            make.top.equalTo(iconImageView.top);
            make.width.equalTo(16);
            make.height.equalTo(nameLabel.height);
        }];

        // 正文
        UILabel *text_Label = [[UILabel alloc] init];
        text_Label.font = XMGTextFont;
        text_Label.numberOfLines = 0;
        [self.contentView addSubview:text_Label];
        // 手动设置label的文字的最大宽度(目的:为了能够计算label的高度,得到最真实的尺寸)
        text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
        self.text_Label = text_Label;
        // 添加约束
        [text_Label makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.left);
            make.top.equalTo(iconImageView.bottom).offset(space);
            make.right.equalTo(self.contentView).offset(-space);
        }];

        // 配图
        UIImageView *pictureImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureImageView];
        self.pictureImageView = pictureImageView;
        // 添加约束
        [pictureImageView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(text_Label.bottom).offset(space);
            make.left.equalTo(iconImageView.left);
            make.width.equalTo(100);
            make.height.equalTo(100);
            // 不能加这句,否则会破坏frame的计算
            make.bottom.equalTo(self.contentView.bottom).offset(-space);
        }];

    }
    return self;
}

// 设置数据
- (void)setStatus:(XMGStatus *)status
{
    _status = status;
    self.iconImageView.image = [UIImage imageNamed:status.icon];

    self.nameLabel.text = status.name;

    if (status.isVip) { // 是VIP
        self.vipImageView.hidden = NO;
        self.nameLabel.textColor = [UIColor orangeColor];
    } else {
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }

    self.text_Label.text = status.text;

    if (status.picture) { // 有配图
        // 更新约束
        [self.pictureImageView updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(100);
        }];
        [self.pictureImageView updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.bottom).offset(-space);
        }];
        // 设置图片
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else {
        // 更新约束
        [self.pictureImageView updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(0);
        }];
        [self.pictureImageView updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(0);
        }];
    }
}

@end

你可能感兴趣的:([自定义不等高的cell]-纯代码Autolayout方式iOS8之后)