您的位置:首页 > 产品设计 > UI/UE

UITableViewCell AutoLayout 动态行高

2017-08-31 15:51 423 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息