【问题标题】:Self growing label in tableview cell表格视图单元格中的自增长标签
【发布时间】:2017-09-27 04:16:12
【问题描述】:

我正在与TableviewObjective C 合作。

我在 TableView 单元格中有 4 个标签。我想根据内容大小扩展评论正文标签和答案正文标签。如何使用 AutoLayout。请帮帮我

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

tableview 的截图如下。

【问题讨论】:

    标签: ios objective-c uitableview uilabel tableview


    【解决方案1】:

    你的约束应该是这样的,

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

    Answer -> 顶部,前导,固定宽度,固定高度

    Comment Body -> 顶部、前导、尾部(右侧)、固定高度(大于或等于某个常数值)、底部

    Answer Body -> 顶部、前导、尾随、底部

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

    你必须为你的tableview设置estimatedrowheight和row height,比如,

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

    【讨论】:

      【解决方案2】:

      约束

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

      答案正文:- 顶部、尾随、底部

      comment :- 中心 y 到评论正文,前导,水平到评论正文,固定宽度

      答案:- 中心 y 到答案正文,领先,水平到答案正文,固定宽度

      numberOfLine 0 用于两个动态标签

      代码

      将以下代码添加到ViewDidLoadViewWillAppear

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

      注意:删除heightForRow

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多