ios - UITableView 自动维度无法正常工作
问题描述
我有一个表格视图,其中填充了来自 Firebase 的数据。但是,当使用自动尺寸调整 tableview 的大小时,一些文本显示被截断。
这是我的故事板,其约束设置为顶部、底部、右侧和左侧。
当此处显示的文本不多时,它工作正常。
但是,当我用大量文本填充单元格时,就会发生这种情况。
这是我目前正在使用的代码。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (tableView == questInformationTableView) {
return 50
}
else if (tableView == questWalkthroughTableView) {
return UITableView.automaticDimension
}
return 0
}
override func viewDidLoad() {
super.viewDidLoad()
questWalkthroughTableView.estimatedRowHeight = 250
questWalkthroughTableView.rowHeight = UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
我还将 numberOfLines 设置为 0 并将标签设置为 .sizeToFit()
解决方案
我通常设置自动高度尺寸,然后像这样在 viewDidLoad 中重新加载
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.reloadData()
}
推荐阅读
- c++ - 我可以从超类对象的向量中获取子类对象并将其推入子类向量吗?
- javascript - JavaScript找到最长的回文比较数组不起作用
- python - 运行我自己的脚本的命令行参数
- javascript - 按名称获取 cookie,并替换一些符号
- mysql - 为什么我不能在 MySQL bench 中使用“source”?
- android - 当列表中有太多项目时,如何在列表视图下方的屏幕上保留一个按钮?
- android - 具有全屏活动的启动画面未按预期工作
- c# - MQQueueManager 的初始化在 .NET core 2.2 中挂起
- python - /accounts/register 处的 ValueError 必须设置给定的用户名
- statistics - 频率分析