繁体   English   中英

如何在Swift 3中计算或自动调整UiTableView的高度

[英]How to calculate or auto fit height of UiTableView in Swift 3

我的TableView内容3控制器。

1)WebView 1

2)ImageView

3)Webview 2

我正在动态设置所有3个控制器的帧大小。 因此,应根据内容大小更改行高。

                    self.secondWebViewLable.frame = CGRect(x: 0,
                                                       y: Int(self.firstImage.frame.origin.y + self.firstImage.frame.height),
                                                       width: Int(screenSize.width),
                                                       height: Int(self.secondWebViewLable.frame.height) )
self.firstImage.frame  = CGRect(x: 0,
                                                        y: Int(self.firstWebViewLable.frame.origin.y + self.firstWebViewLable.frame.height),
                                                        width: Int(screenSize.width),
                                                        height: Int(scaledHeight) )

您会看到控制器的高度正在根据数据而变化。 我也在尝试设置单元格的高度。 下面是我的代码。 但是我固定了高度。 请帮助我。

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

      return screenSize.height
    }

使用自动布局

viewDidLoad()中设置估计的行高,并从heightForRowAt indexPath方法返回UITableViewAutomaticDimension

请参阅本教程以获取更多详细说明: self-sizing-table-view-cells

没有自动布局

如果没有自动布局,则必须通过将单元格中所有UI组件的高度和边距相加来计算每个单元格的高度,并在UITableView数据源方法tableView:heightForRowAtIndexPath中返回计算出的高度

暂无
暂无

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

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