UITableViewCell AutoLayout 動(dòng)態(tài)行高(iOS 8.0+)
最近有空整理記錄一下在AutoLayout中如何使UITableViewCell的行高根據(jù)內(nèi)容(以UILabel的多行顯示為例)達(dá)到自適應(yīng)高度。
如下圖:cell 高度沒(méi)有動(dòng)態(tài)設(shè)置,label的文字沒(méi)有顯示全从撼,
1、首先在自定義的cell中速挑,設(shè)置好所需要的約束。
這里寫圖片描述
2塔插、保持默認(rèn)狀態(tài):Row Height 是Default而不是custom的數(shù)值梗摇,否則之后不管你如何操作拓哟。
這里寫圖片描述
3想许、還有一點(diǎn)要注意的是,UILabel的行數(shù)要設(shè)置為0断序,表示UILabel顯示的是多行流纹。
這里寫圖片描述
4、最后實(shí)現(xiàn)UITableView的代理:
//設(shè)置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;//自動(dòng)尺寸
}
//預(yù)估行高
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44;
}