【问题标题】:Automatic height for TextView using autolayout in cell for tableView在表格视图的单元格中使用自动布局的 TextView 的自动高度
【发布时间】:2019-05-01 12:44:49
【问题描述】:

我想根据表格视图单元格中的内容设置 TextView 的高度。

我将文本视图的顶部固定到职位 UILabel,底部固定到内容视图(父级),但它不起作用。有这种布局定位的教程吗?

如何添加约束来解决自动高度问题?

当前限制

【问题讨论】:

    标签: swift autolayout storyboard tableview


    【解决方案1】:

    我可以看到您在单元格中还有一些其他设置。支持动态高度

    1. 您可以为 UITextView 提供固定高度约束。
    2. 摆脱这种约束。
    3. 在从cellForRowAt 方法返回单元格之前更新它。

    所以,你的 cellForRowAt 方法最终会像

    tableCell.textViewHeightConstraint = textView.contentSize.height
    tableCell.setNeedsUpdateConstraints()
    tableCell.setNeedsLayout()
    return tableCell
    

    【讨论】:

      【解决方案2】:

      这样解决了。 将TextView更改为UILabel,然后将属性检查器中的Lines属性设置为0。

      关于 viewDidLoad

          tableview.rowHeight = UITableView.automaticDimension
          tableview.estimatedRowHeight = 229.0
      

      有效。

      更多https://www.raywenderlich.com/8549-self-sizing-table-view-cells

      【讨论】:

      • 很高兴你解决了它。但是 textView 有一些功能,而 label 没有。
      【解决方案3】:

      您可以通过添加两行来应用 Self-Sizing Table View Cells:

      tableView.estimatedRowHeight = 85.0
      tableView.rowHeight = UITableViewAutomaticDimension
      

      您应该阅读 Apple 提供的这份文档: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html

      【讨论】:

      • 是因为单元格高度没有扩大还是因为TextView的约束不对?
      • tableview.estimatedRowHeight = 85.0 tableview.rowHeight = UITableView.automaticDimension 不起作用
      • 你还在用 cellForRowAtIndexPath 吗?
      • tableView:heightForRowAtIndexPath: 怎么样?你在用吗?
      • 通过将 textview 更改为 uiLabel 并将 Lines 属性设置为零来解决问题。然后自动尺寸工作,谢谢大家
      猜你喜欢
      • 2013-05-16
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      相关资源
      最近更新 更多