tableview细胞中的自我增长标签

时间:2017-09-27 04:16:13

标签: ios objective-c uitableview uilabel tableview

我正与Tableview中的Objective C合作。

我在TableView单元格中有4个标签。我想根据内容大小扩展Comment Body标签和Answer Body标签。如何使用AutoLayout。请帮帮我

如何根据内容设置标签的动态高度?

tableview的ScreenShot如下所示。

enter image description here

2 个答案:

答案 0 :(得分:0)

约束

评论正文: - 顶部,尾随,底部

答案正文: - 顶部,尾部,底部

评论: - 中心y到注释主体,前导,水平到注释主体,修复宽度

答案: - 中心y回答身体,领先,水平到答案身体,修复宽度

numberOfLine 0表示动态标签

<强>代码

将以下代码添加到ViewDidLoadViewWillAppear

 self.tableView.rowHeight = UITableViewAutomaticDimension;
 self.tableView.estimatedRowHeight = 80;

注意:删除heightForRow

答案 1 :(得分:0)

你的约束应该是,

Comment - &gt;顶部,前导(左侧),固定宽度,固定高度

Answer - &gt;顶部,前导,固定宽度,固定高度

Comment Body - &gt;顶部,前导,尾随(右侧),固定高度(大于或等于某个常数值),底部

Answer Body - &gt;顶部,领先,尾随,底部

确保comment bodyanswer body的行数必须为0

你必须为你的桌面视图设置估算高度和行高,

  self.yourTableView.estimatedRowHeight = 50;
  self.yourTableView.rowHeight = UITableViewAutomaticDimension;

OR

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

      return 50;
  }

 - (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

     return UITableViewAutomaticDimension;
}
相关问题