简体   繁体   English

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

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

My TableView contents 3 controller. 我的TableView内容3控制器。

1) WebView 1 1)WebView 1

2) ImageView 2)ImageView

3) Webview 2 3)Webview 2

I am doing dynamically set the frame size of all 3 controller. 我正在动态设置所有3个控制器的帧大小。 So row height should be changed according of content sizes. 因此,应根据内容大小更改行高。

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

you can see that controllers height is changing based on data. 您会看到控制器的高度正在根据数据而变化。 I am trying to set the height of cell also. 我也在尝试设置单元格的高度。 below is my code. 下面是我的代码。 but i fixed the height. 但是我固定了高度。 kindly help me. 请帮助我。

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

      return screenSize.height
    }

With Autolayout 使用自动布局

Set the estimated row height in viewDidLoad() and Return UITableViewAutomaticDimension from heightForRowAt indexPath method. viewDidLoad()中设置估计的行高,并从heightForRowAt indexPath方法返回UITableViewAutomaticDimension

Refer this tutorial for more detailed explanation: self-sizing-table-view-cells 请参阅本教程以获取更多详细说明: self-sizing-table-view-cells

Without Autolayout 没有自动布局

Without Autolayout you have to calculate height for each cell by adding the heights of all the UI components in the cell and margins and return that calculated height in UITableView datasource method tableView:heightForRowAtIndexPath: 如果没有自动布局,则必须通过将单元格中所有UI组件的高度和边距相加来计算每个单元格的高度,并在UITableView数据源方法tableView:heightForRowAtIndexPath中返回计算出的高度

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

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