自动计算UITableViewCell高度2(CGRect约束)

1.先创建model

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface LBDNewMsgListModel : NSObject

@property (nonatomic, copy) NSString *activityId;
@property (nonatomic, copy) NSString *activityImg;
@property (nonatomic, copy) NSString *colorType;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *createdAt;
@property (nonatomic, copy) NSString *remark;
@property (nonatomic, copy) NSString *jump;
@property (nonatomic, copy) NSString *title;

@property (assign,nonatomic) CGFloat cellHeight; // cell的高度(在本model类中进行计算)

@end

.m

#import "LBDNewMsgListModel.h"
#import "LBDMsgCenterCell.h"

@interface LBDNewMsgListModel ()

@property (strong, nonatomic) LBDMsgCenterCell *cell;

@end

@implementation LBDNewMsgListModel

- (LBDMsgCenterCell *)cell
{
    
    if(!_cell){
        
        _cell = [[LBDMsgCenterCell alloc]init];
        _cell.model = self;
    }
    return _cell;
}

- (CGFloat)cellHeight
{
    if (_cellHeight == 0) {
        
        _cellHeight = self.cell.cellHeight;
        self.cell = nil;
    }
    return _cellHeight;
}

2.自定义cell

.h

#import <UIKit/UIKit.h>
#import "LBDNewMsgListModel.h"

static NSString *cellID = @"LBDMsgCenterCell";

@protocol LBDMessageCenterCellDelagete <NSObject>

@optional

- (void)returnCellHeight:(CGFloat)height;

@end

@interface LBDMsgCenterCell : UITableViewCell

@property (nonatomic, strong) UILabel *msgDateLbl;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIImageView *msgImgView;
@property (nonatomic, strong) UILabel *titleLbl;
@property (nonatomic, strong) UILabel *orderNum;
@property (nonatomic, strong) UILabel *contentLbl;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *lookLbl;

@property (nonatomic, strong) LBDNewMsgListModel *model;
+ (instancetype)cellWithTableView:(UITableView *)tableView;

@property (nonatomic, weak) id<LBDMessageCenterCellDelagete>delegate;

@property (assign,nonatomic) CGFloat cellHeight; // cell的高度

@end

.m

#import "LBDMsgCenterCell.h"
#import "LBDNewMsgListModel.h"
#import "NSString+Extension.h"

@implementation LBDMsgCenterCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    LBDMsgCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        
        cell = [[LBDMsgCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = [UIColor getColor:@"ececec"];
    cell.backgroundColor = [UIColor getColor:@"ececec"];
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self drawCell];
    }
    return self;
}

- (void)setModel:(LBDNewMsgListModel *)model
{
    _model = model;
    
    if ([model.colorType isEqualToString:@"1"]) {
        
        _topLineView.hidden = NO;
        _msgImgView.hidden = YES;
        _topLineView.backgroundColor = [UIColor getColor:@"feb540"];
    } else if ([model.colorType isEqualToString:@"2"]) {
        
        _topLineView.hidden = NO;
        _msgImgView.hidden = YES;
        _topLineView.backgroundColor = [UIColor getColor:@"00ade5"];
    } else if ([model.colorType isEqualToString:@"3"]) {
        
        _topLineView.hidden = YES;
        _msgImgView.hidden = NO;
        
        [_msgImgView sd_setImageWithURL:[NSURL URLWithString:model.activityImg]];
        
    }
    
    if ([model.jump isEqualToString:@"0"]) {
        
        _lineView.hidden = YES;
        _lookLbl.hidden = YES;
    } else if ([model.jump isEqualToString:@"1"]) {
        
        _lineView.hidden = NO;
        _lookLbl.hidden = NO;
    }
    
    _msgDateLbl.text = [Tool dateStrConverWithDateStr:model.createdAt originalType:@"yyyyMMddHHmmss" type:@"yyyy/MM/dd HH:mm"];
    _titleLbl.text = model.title;
    _contentLbl.text = model.content;
    
    
    [self layoutIfNeeded];
    self.cellHeight = CGRectGetMaxY(_bgView.frame);
    
}

- (void)drawCell {
    
    _msgDateLbl = [[UILabel alloc] init];
    _msgDateLbl.text = @"2016/12/25 09:11";
    _msgDateLbl.font = FONT(10);
    _msgDateLbl.textColor = [UIColor getColor:@"646464"];
    _msgDateLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_msgDateLbl];
    
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 10;
    [self.contentView addSubview:_bgView];
    
    _msgImgView = [[UIImageView alloc] init];
    _msgImgView.hidden = YES;
    _msgImgView.contentMode = UIViewContentModeScaleAspectFit;
    [_bgView addSubview:_msgImgView];
    
    _topLineView = [[UIView alloc] init];
    _topLineView.hidden = YES;
    [_bgView addSubview:_topLineView];
    
    _titleLbl = [[UILabel alloc] init];
    _titleLbl.font = FONT(15);
    _titleLbl.numberOfLines = 0;
    _titleLbl.textColor = [UIColor getColor:@"323232"];
    [_bgView addSubview:_titleLbl];
    
    _contentLbl = [[UILabel alloc] init];
    _contentLbl.font = FONT(12);
    _contentLbl.numberOfLines = 0;
    _contentLbl.textColor = [UIColor getColor:@"969696"];
    [_bgView addSubview:_contentLbl];
    
    _lookLbl = [[UILabel alloc] init];
    _lookLbl.textAlignment = NSTextAlignmentRight;;
    _lookLbl.text = @"立即查看";
    _lookLbl.font = FONT(13);
    [_bgView addSubview:_lookLbl];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor getColor:@"ececec"];
    [_bgView addSubview:_lineView];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _msgDateLbl.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
    
    _bgView.frame = CGRectMake(10, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - 20, 100);
    
    CGSize titleSize = [_model.title sizeWithFont:FONT(15) andMaxSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT)];
    CGSize contentSize = [_model.content sizeWithFont:FONT(12) andMaxSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT)];
    
    if ([_model.colorType isEqualToString:@"1"] || [_model.colorType isEqualToString:@"2"]) {
        
        _topLineView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 20, 5);
        _msgImgView.frame = CGRectZero;
        
        _titleLbl.frame = CGRectMake(10, CGRectGetMaxY(_topLineView.frame) + 10, SCREEN_WIDTH - 40, titleSize.height);
        
    } else if ([_model.colorType isEqualToString:@"3"]) {
        
        _topLineView.frame = CGRectZero;
        _msgImgView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 20, 110);
        
        _titleLbl.frame = CGRectMake(10, CGRectGetMaxY(_msgImgView.frame) + 10, SCREEN_WIDTH - 40, titleSize.height);
        
    }
    
    _contentLbl.frame = CGRectMake(10, CGRectGetMaxY(_titleLbl.frame),  SCREEN_WIDTH - 40, contentSize.height + 30);
    
    if ([_model.jump isEqualToString:@"0"]) {
        
        _lineView.frame = CGRectZero;
        _lookLbl.frame = CGRectZero;
        
        _bgView.frame = CGRectMake(10, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - 20, CGRectGetMaxY(_contentLbl.frame));
        
    } else if ([_model.jump isEqualToString:@"1"]) {
        
        _lineView.frame = CGRectMake(0, CGRectGetMaxY(_contentLbl.frame), SCREEN_WIDTH - 20, 0.5);
        _lookLbl.frame = CGRectMake(0, CGRectGetMaxY(_lineView.frame), SCREEN_WIDTH - 28, 30);
        
        _bgView.frame = CGRectMake(10, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - 20, CGRectGetMaxY(_lookLbl.frame));
        
    }
    
}

3.在控制器中使用

.m

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LBDNewMsgListModel *model = self.dataMutArr[indexPath.row];
    return model.cellHeight;
}
原文地址:https://www.cnblogs.com/xuzb/p/8706899.html