首页 > 解决方案 > 调用 Firebase 后自行调整单元格大小

问题描述

我正在构建一个表格视图,我正在加载不同大小的文本并希望单元格是动态的。当我不先调用数据库时,这非常简单。我知道我正确地使用了约束,因为当我没有调用数据库并且只是创建了一个数组时,我让单元格正常运行。我可以让它工作的唯一方法是在 viewDidLoad() 中调用我的函数来获取数据,然后在闭包中重新加载 tableview。然后在 vewDidAppear 中,我再次重新加载 tableView。这可行,但从用户体验方面来看似乎很难看,因为文本加载一次,然后调整大小。还有其他我不知道的方法吗?先感谢您!

这是我的 viewDidLoad() 和 viewDidAppear() 这个控制器中的所有其他内容都是非常基本的。

override func viewDidLoad() {
    super.viewDidLoad()
    setupNavigationBarItems()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    commentTextView.delegate = self
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "yyyy-MM-dd HH:mm"

    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableView.automaticDimension
    
    
    comment.getComments(roundID: roundID) {
        self.tableView.reloadData()
    }
    
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    self.tableView.reloadData()
}

标签: swiftfirebasetableview

解决方案


推荐阅读