【发布时间】:2019-05-01 12:44:49
【问题描述】:
我想根据表格视图单元格中的内容设置 TextView 的高度。
我将文本视图的顶部固定到职位 UILabel,底部固定到内容视图(父级),但它不起作用。有这种布局定位的教程吗?
如何添加约束来解决自动高度问题?
【问题讨论】:
标签: swift autolayout storyboard tableview
我想根据表格视图单元格中的内容设置 TextView 的高度。
我将文本视图的顶部固定到职位 UILabel,底部固定到内容视图(父级),但它不起作用。有这种布局定位的教程吗?
如何添加约束来解决自动高度问题?
【问题讨论】:
标签: swift autolayout storyboard tableview
我可以看到您在单元格中还有一些其他设置。支持动态高度
cellForRowAt 方法返回单元格之前更新它。所以,你的 cellForRowAt 方法最终会像
tableCell.textViewHeightConstraint = textView.contentSize.height
tableCell.setNeedsUpdateConstraints()
tableCell.setNeedsLayout()
return tableCell
【讨论】:
这样解决了。 将TextView更改为UILabel,然后将属性检查器中的Lines属性设置为0。
关于 viewDidLoad
tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0
有效。
更多https://www.raywenderlich.com/8549-self-sizing-table-view-cells
【讨论】:
您可以通过添加两行来应用 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
【讨论】: