IOS tableView cell动态高度 (autoLayout)

要做到使用autoLayout来生成动态高度的cell.cell里边子视图控件要使用autoLayout.

1.比如我定义的cell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        // Initialization code
        [self configUserInterface];
    }

    return self;
}

- (void)configUserInterface
{
    UIImageView *bgImageV = [[UIImageView alloc]init];

    bgImageV.image = [[UIImage imageNamed:@"activityCellBox"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self addSubview:bgImageV];
    [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(12 * GY_Ratio));
        make.right.equalTo(self.mas_right).offset(-12 * GY_Ratio);
        make.top.equalTo(@(15 * GY_Ratio));
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];

    _mainContentImageV = [[UIImageView alloc]init];
    [bgImageV addSubview:_mainContentImageV];
    [_mainContentImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(bgImageV.mas_right).offset(0);
        make.top.equalTo(bgImageV.mas_top);
        make.height.equalTo(@(70 * GY_Ratio));
    }];
    _activityContentLabel = [[UILabel alloc]init];
    _activityContentLabel.numberOfLines = 0;
    [bgImageV addSubview:_activityContentLabel];
    [_activityContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(bgImageV.mas_right).offset(0);
        make.top.equalTo(_mainContentImageV.mas_bottom).offset(5).priorityHigh();
        make.bottom.equalTo(self.mas_bottom).offset(-1);
    }];
    _activityContentLabel.font = [UIFont systemFontOfSize:14 * GY_Ratio];
    _activityContentLabel.textColor = [UIColor colorFromHexString:@"#575656"];

    _activityStatusImageV = [[UIImageView alloc]init];
    [bgImageV addSubview:_activityStatusImageV];
    _activityStatusImageV.image = [UIImage imageNamed:@"activityStartingBg"];
    [_activityStatusImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgImageV.mas_right).offset(-10 * GY_Ratio);
        make.bottom.equalTo(_mainContentImageV.mas_bottom).offset(0);
        make.size.equalTo(_activityStatusImageV);
    }];

    _activityStatusLabel = [[UILabel alloc]init];
    [_activityStatusImageV addSubview:_activityStatusLabel];
    [_activityStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_activityStatusImageV.mas_left);
        make.centerY.equalTo(_activityStatusImageV.mas_centerY);
        make.right.equalTo(_activityStatusImageV.mas_right).offset(0);
        make.height.equalTo(@(20 * GY_Ratio));
    }];
    _activityStatusLabel.text = @"未知";
    _activityStatusLabel.textColor = [UIColor whiteColor];
    _activityStatusLabel.font = [UIFont systemFontOfSize:10 * GY_Ratio];
    _activityStatusLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)setContentWithModel:(GYActivityCenterModel *)model andPicSavePath:(NSString *)picSavePath
{
    NSString *picPathStr = [NSString stringWithFormat:@"%@/%@%@", API_ServerAddress, picSavePath, model.picName];

    [_mainContentImageV sd_setImageWithURL:[NSURL URLWithString:picPathStr] placeholderImage:[UIImage imageNamed:@"isWinningBg"]];

    _activityContentLabel.text = model.listDescription;
    float needHeight = [self getLabelHeigth:_activityContentLabel];

    [_activityTitleLabel layoutIfNeeded];
    [_activityContentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(needHeight));
    }];
}

#pragma mark - 获取label字符串所占的高度
- (float)getLabelHeigth:(UILabel *)label
{
    CGSize size = [label.text boundingRectWithSize:(CGSize) {GY_MainWidth - 24 * GY_Ratio, 1000}
        options     :NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
        attributes  :@{NSFontAttributeName : label.font}
        context     :nil].size;

    return size.height + 5;
}

这里_activityContentLabel这个label可能所占用的高度不确定.所以在设置cell内容时,计算出其_activityContentLabel所占用的高度.其他控件的布局由于设置好的约束会随着改变布局.


2.我们要在tableViewDelegate 返回高度那儿

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYActivityCenterCell *cell = (GYActivityCenterCell *)[tableView dequeueReusableCellWithIdentifier:cellIntifier];
    GYActivityCenterModel *model = dataSourceArray[indexPath.row];
    [cell setContentWithModel:model andPicSavePath:activityCenterModelLsit.savePath]; ///这里是设置填充内容到cell中.
    [cell layoutIfNeeded];
    CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;///这里就得到cell的实际高度. 
    return height;
}




你可能感兴趣的:(iOS)