简体   繁体   中英

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

My TableView contents 3 controller.

1) WebView 1

2) ImageView

3) Webview 2

I am doing dynamically set the frame size of all 3 controller. 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.

Refer this tutorial for more detailed explanation: 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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