简体   繁体   English

Swift UITableView自动高度

[英]Swift UITableView auto height

I have a design, where UITableView is in UIScrollView with other content and the content is scrolling with the table data. 我有一个设计,其中UITableView与其他内容一起位于UIScrollView中,并且内容与表格数据一起滚动。 It means, that table has scrolling disabled and is stretched to full size. 这意味着该表已禁用滚动,并且已拉伸到完整大小。

I'm using autolayout and the table has height constraint, set to some default value. 我正在使用自动布局,并且表格具有高度限制,设置为一些默认值。

Then I have overwritten viewWillLayoutSubviews method: 然后,我覆盖了viewWillLayoutSubviews方法:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    if (self.itemTableView.contentSize.height > 0) {
        self.itemTableViewHeightConstraint.constant = self.itemTableView.contentSize.height
    }
}

With this method in place, when I called self.itemTableView.reloadData() after loading the data from the REST, the height constraint was set to table content height, which was great. 使用此方法后,当我从REST加载数据后调用self.itemTableView.reloadData()时,将高度限制设置为表内容的高度,这很棒。

This used to work for me in many projects, but for some reason it is not working in the latest one, written in swift 4. Does something changed? 在许多项目中,这曾经对我有用,但是由于某种原因,它在迅速编写的最新项目中却没有用。4。是否有所改变? Why tableView reloadData() method does not invoke viewWillLayoutSubviews()? 为什么tableView reloadData()方法不调用viewWillLayoutSubviews()?

问题是tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)在返回行之前未调用self.view.setNeedsLayout()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM