iOS UITableViewCell高度自適應(yīng)

一、高度自適應(yīng)

高度自適應(yīng)這個功能比較簡單了,滿足下面三點(diǎn)即可:

  • 1、設(shè)置UITableViewrowHeightUITableViewAutomaticDimension,如:

    _tableView.rowHeight = UITableViewAutomaticDimension;/// 高度自適應(yīng)
    
  • 2、移除UITableViewDelegate的代理heightForRowAtIndexPath,就是下面的代碼不要出現(xiàn)在頁面內(nèi)。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    }
    
  • 3、設(shè)置好UITableViewCell內(nèi)的控件約束。如:

    UILabel *label = [[UILabel alloc] init];
    [self.contentView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(8);
        make.left.mas_equalTo(self.contentView).offset(12);
        make.right.mas_equalTo(self.contentView).offset(-12);
        make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
        make.bottom.mas_equalTo(self.contentView).offset(-16);
    }];
    

二、遇到的問題 & 解決方法

1、約束警告

[LayoutConstraints] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
****
)
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

上面的問題是由于系統(tǒng)判定代碼和編輯器中可能出現(xiàn)了重復(fù)約束,可以不做處理,跳過!解決辦法就是給有問題的約束**設(shè)置優(yōu)先級**。

為什么會出現(xiàn)重復(fù)約束呢?如第一步中第三點(diǎn),關(guān)于label的約束,正常情況下設(shè)置top、left、bottom、right已經(jīng)可以確定一個控件的位置了,但是這里還設(shè)置了height的約束,這個height就是重復(fù)約束,給其設(shè)定優(yōu)先級priorityHigh()即可。

2、UILabel內(nèi)容展示不全

需求是完整展示多行文本。代碼按常規(guī)方式寫完,但實(shí)際測試結(jié)果卻是在部分機(jī)型上,內(nèi)容展示不全,顯示...

解決方法:在設(shè)置完fontnumberOfLines之后,再設(shè)置adjustsFontSizeToFitWidth。

但是在UITableViewUITableViewCell上的控件,設(shè)置完adjustsFontSizeToFitWidth之后,發(fā)現(xiàn)內(nèi)容還是顯示不全。這個時候需要額外設(shè)置preferredMaxLayoutWidth才能解決這個問題。

最終的效果就是:

UILabel *label = [[UILabel alloc] init];
label.font = PFFont_Regular(14);
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = 200;
label.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:label];

3、多個控件同時約束bottom

這里以簡單情況為例:

效果一 效果二

如上面表格所示,由于UILabel內(nèi)容的不確定性,UITableViewCell的高度需要根據(jù)多個控件來確定,可能是第一個控件,也可能是第二個控件。做法也比較簡單給兩個控件分別添加bottom約束。

/// 內(nèi)容1
UILabel *label1 = [[UILabel alloc] init];
label1.font = PFFont_Regular(14);
label1.numberOfLines = 0;
[self.contentView addSubview:label1];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
    make.left.mas_equalTo(self.contentView).offset(15);
    make.width.mas_equalTo(100);
    make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
    make.bottom.mas_equalTo(self.contentView).offset(-15);
}];

/// 內(nèi)容2
UILabel *label2 = [[UILabel alloc] init];
label2.font = PFFont_Regular(14);
label2.numberOfLines = 0;
[self.contentView addSubview:label2];

[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(self.contentView.mas_bottom).offset(15);
    make.right.mas_equalTo(self.contentView).offset(-15);
    make.width.mas_equalTo(100);
    make.height.mas_greaterThanOrEqualTo(20).priorityHigh();
    make.bottom.mas_equalTo(self.contentView).offset(-15);
}];

但是在實(shí)際情況下,上面情況中兩個UILabel的高度其實(shí)是一致的,這種情況下我的解決辦法是給Label文本居頂,具體做法參考文章

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

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

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