OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView 自动维度无法正常工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:49
标题: ios - UITableView 自动维度无法正常工作

我有一个使用 Firebase 数据填充的 tableview。 但是,当使用自动尺寸调整 tableview 的大小时,会显示一些文本被截断。

这是我的 Storyboard,其约束设置为顶部、底部、右侧和左侧。

enter image description here

当此处显示的文本不多时,它可以正常工作。

enter image description here

但是,当我用大量文本填充单元格时,就会发生这种情况。

enter image description here

这是我目前正在使用的代码。

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()



Best Answer-推荐答案


不要将底部约束设置为大于或等于顶部约束。去掉 estimatedHeightForRowAtheightForRowAt 函数。您的观点确实加载声明就足够了。当你重新加载数据时也调用 self.view.layoutIfNeeded

关于ios - UITableView 自动维度无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325013/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4