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;
}