TableViewCell自適應(yīng)高度三種實(shí)現(xiàn)方式

實(shí)現(xiàn)cell的自適應(yīng)布局的前提是要讓cell中的子空間的布局要使用autolayout,這篇文章我們會(huì)通過(guò)Masonry、SDAutoLayout、xib三種方式實(shí)現(xiàn)布局的autolayout,再通過(guò)Y軸方向的完全約束來(lái)實(shí)現(xiàn)cell高度自適應(yīng)。
1.Masonry實(shí)現(xiàn)cell自適應(yīng):
(1)首先通過(guò)Masonry實(shí)現(xiàn)cell的布局

  • (void)setUpView{
    ...此處布局省略
    [self.contentView addSubview:self.adavterImgV];
    [_adavterImgV mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(headerView.mas_bottom).offset(15);
    make.left.equalTo(headerView).offset(15);
    make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [self.contentView addSubview:self.contentImgV];
    [_contentImgV mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.nameLabel.mas_bottom).offset(15);
    make.left.equalTo(self.contentView).offset(15);
    make.right.equalTo(self.contentView).offset(-15);
    make.height.equalTo(@200);
    }];

    [self.contentView addSubview:self.contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentImgV.mas_bottom).offset(15);
    make.left.equalTo(self.contentView).offset(15);
    make.right.equalTo(self.contentView).offset(-15);
    make.bottom.equalTo(self.contentView).offset(-15);
    }];
    }
    //只自適應(yīng)文本內(nèi)容

pragma mark -Setter

  • (void)setViewModel:(SGITestModel *)viewModel{
    [self.contentImgV setImage:[UIImage imageNamed:viewModel.imageName]];
    [self.contentLabel setText:viewModel.content];
    }
    //自適應(yīng)文本和圖片

pragma mark -Setter

  • (void)setViewModel:(SGITestModel *)viewModel{
    [self.contentImgV setImage:[UIImage imageNamed:viewModel.imageName]];
    [self.contentLabel setText:viewModel.content];
    [self.contentImgV mas_updateConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@((SGIScreenWidth-30)/viewModel.ratio));
    }];
    }
    附注:viewModel.ratio為計(jì)算出的圖片寬高比,這里為了方便省略了計(jì)算方式,下面使用的viewModel.ratio同理

(2)通過(guò)預(yù)設(shè)tableview的高度estimatedRowHeight屬性以及tableView的rowHeight設(shè)置UITableViewAutomaticDimension來(lái)實(shí)現(xiàn)高度自適應(yīng)

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
    }
  • (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0f;
    }

核心代碼:self.rowHeight = UITableViewAutomaticDimension;
self.estimatedHeight = 44.0f(隨便預(yù)設(shè)高度)

(3)效果如下:


IMG_0158.JPG

2.SDAutoLayout實(shí)現(xiàn)cell的自適應(yīng)

  • (void)setUpView{
    ...此處布局省略

    [self.contentView addSubview:self.contentImgV];
    _contentImgV.sd_layout
    .topSpaceToView(self.nameLabel, 15)
    .leftEqualToView(self.adavterImgV)
    .rightEqualToView(self.nameLabel)
    .heightIs(200);

    [self.contentView addSubview:self.contentLabel];
    _contentLabel.sd_layout
    .topSpaceToView(self.contentImgV, 15)
    .leftEqualToView(self.contentImgV)
    .rightEqualToView(self.contentImgV)
    .autoHeightRatio(0);
    [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:15];

}
//只自適應(yīng)文本內(nèi)容

pragma mark -Setter

  • (void)setViewModel:(SGITestModel *)viewModel{
    [self.contentImgV setImage:[UIImage imageNamed:viewModel.imageName]];
    [self.contentLabel setText:viewModel.content];
    }
    //自適應(yīng)文本和圖片

pragma mark -Setter

  • (void)setViewModel:(SGITestModel *)viewModel{
    [self.contentImgV setImage:[UIImage imageNamed:viewModel.imageName]];
    [self.contentLabel setText:viewModel.content];
    _contentImgV.sd_resetLayout
    .topSpaceToView(self.nameLabel, 15)
    .leftEqualToView(self.adavterImgV)
    .rightEqualToView(self.nameLabel)
    .autoHeightRatio(viewModel.ratio);
    [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:15];
    }
    //設(shè)置cell的高度
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // return UITableViewAutomaticDimension;
    return [tableView cellHeightForIndexPath:indexPath model:self.contentArray[indexPath.section] keyPath:@"viewModel" cellClass:[SGITestAutoTableViewCell class] contentViewWidth:SGIScreenWidth] ;
    }

核心代碼:[self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:15];
[tableView cellHeightForIndexPath:indexPath model:self.contentArray[indexPath.section] keyPath:@"viewModel" cellClass:[SGITestAutoTableViewCell class] contentViewWidth:SGIScreenWidth] ;

(3)效果圖同上

3.xib實(shí)現(xiàn)自適應(yīng)布局
(1)使用xib實(shí)現(xiàn)布局


1585905241827.jpg

(2)通過(guò)預(yù)設(shè)tableview的高度estimatedRowHeight屬性以及tableView的rowHeight設(shè)置UITableViewAutomaticDimension來(lái)實(shí)現(xiàn)高度自適應(yīng)

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
    }
  • (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0f;
    }

//自適應(yīng)文本和圖片

pragma mark -Setter

  • (void)setViewModel:(SGITestModel *)viewModel{
    [self.contentImgV setImage:[UIImage imageNamed:viewModel.imageName]];
    [self.contentLabel setText:viewModel.content];
    self.constrait_height.constant = (SGIScreenWidth -30) *viewModel.ratio;
    }

核心代碼:self.rowHeight = UITableViewAutomaticDimension;
self.estimatedHeight = 44.0f(隨便預(yù)設(shè)高度)

(3)效果圖同上

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容