我在Objective C中使用Tableview。
我在一个TableView单元格中有4个标签。我想根据内容大小展开评论正文标签和答案正文标签。使用AutoLayout是如何实现的。请帮帮我
如何根据内容设置标签的动态高度?
tableview的ScreenShot如下所示。

发布于 2017-09-27 12:26:37
约束
注释正文:-顶部,尾部,底部
答案正文:-顶部,尾部,底部
注释:-居中y表示注释正文,前导,水平表示注释正文,固定宽度
答案:-居中y表示答案正文,前导,水平表示答案正文,固定宽度
两个动态标签的numberOfLine 0
代码
将以下代码添加到ViewDidLoad或ViewWillAppear
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;注:删除heightForRow
发布于 2017-09-27 12:48:56
你的约束应该是这样的,
Comment ->顶部,行距(左侧),固定宽度,固定高度
Answer ->顶部,行距,固定宽度,固定高度
Comment Body ->顶部、前导、尾部(右侧)、固定高度(大于或等于某个常量值)、底部
Answer Body ->顶部、前导、尾部、底部
确保comment body和answer body的行数必须为0。
你必须为你的表视图设置预估行宽和行高,
self.yourTableView.estimatedRowHeight = 50;
self.yourTableView.rowHeight = UITableViewAutomaticDimension;或
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}https://stackoverflow.com/questions/46439411
复制相似问题