UITableViewCell AutoLayout 动态行高

xiaoxiao2021-02-28  89

UITableViewCell AutoLayout 动态行高(iOS 8.0+)

最近有空整理记录一下在AutoLayout中如何使UITableViewCell的行高根据内容(以UILabel的多行显示为例)达到自适应高度。 如下图:cell 高度没有动态设置,label的文字没有显示全,

1、首先在自定义的cell中,设置好所需要的约束。

2、保持默认状态:Row Height 是Default而不是custom的数值,否则之后不管你如何操作。

3、还有一点要注意的是,UILabel的行数要设置为0,表示UILabel显示的是多行。

4、最后实现UITableView的代理:

//设置行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewAutomaticDimension;//自动尺寸 } //预估行高 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44; }
转载请注明原文地址: https://www.6miu.com/read-41306.html

最新回复(0)