在 tableView 的单元格中使用自动布局的 TextView 的自动高度
Automatic height for TextView using autolayout in cell for tableView
我想根据表格视图单元格中的内容设置 TextView 的高度。
我将文本视图的顶部固定到职位 UILabel,将底部固定到内容视图(父级),但它不起作用。有没有这种布局定位的教程?
如何添加约束来解决自动高度问题?
当前限制
您可以通过添加两行来应用自我调整 Table 查看单元格:
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
您应该阅读 Apple 的这份文档:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
这样解决了。
将 TextView 更改为 UILabel,然后将属性检查器中的 Lines 属性 设置为 0.
在 viewDidLoad 上
tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0
有效。
更多https://www.raywenderlich.com/8549-self-sizing-table-view-cells
据我所知,您在单元格中还有一些其他设置。支持动态高度
- 你可以给 UITextView 固定高度限制。
- 摆脱这种限制。
- 在 return 来自
cellForRowAt
方法的单元格之前更新它。
所以,您的 cellForRowAt 方法最终会像
tableCell.textViewHeightConstraint = textView.contentSize.height
tableCell.setNeedsUpdateConstraints()
tableCell.setNeedsLayout()
return tableCell
我想根据表格视图单元格中的内容设置 TextView 的高度。
我将文本视图的顶部固定到职位 UILabel,将底部固定到内容视图(父级),但它不起作用。有没有这种布局定位的教程?
如何添加约束来解决自动高度问题?
当前限制
您可以通过添加两行来应用自我调整 Table 查看单元格:
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
您应该阅读 Apple 的这份文档: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
这样解决了。 将 TextView 更改为 UILabel,然后将属性检查器中的 Lines 属性 设置为 0.
在 viewDidLoad 上
tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0
有效。
更多https://www.raywenderlich.com/8549-self-sizing-table-view-cells
据我所知,您在单元格中还有一些其他设置。支持动态高度
- 你可以给 UITextView 固定高度限制。
- 摆脱这种限制。
- 在 return 来自
cellForRowAt
方法的单元格之前更新它。
所以,您的 cellForRowAt 方法最终会像
tableCell.textViewHeightConstraint = textView.contentSize.height
tableCell.setNeedsUpdateConstraints()
tableCell.setNeedsLayout()
return tableCell