1

I am implementing uitableview, where I have 5 cells in total (each is having webview in it with dynamic text). I am implementing 2 xib in this tableview.

But I am having problems with adjusting the height of tableview

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return UITableView.automaticDimension . // is returning a -1 value
    }

I have already set these properties before reloading my tableview

self.myTableview.rowHeight = UITableView.automaticDimension
self.myTableview.estimatedRowHeight = 300

But not able to calculate the height dynamically. I have cross-checked the constraints in xib file and attaching the constraints which I have applied to tableview.

Any suggestions on how I can solve this?

enter image description here

arinjay
  • 83
  • 7
  • `UITableView.automaticDimension` is a constant ... `-1`. It tells `UIKit` that you want to *auto-layout* your views, based on your constraints. ***DO NOT*** implement `heightForRowAt` unless you want to return specific values. – DonMag Aug 27 '19 at 12:07

1 Answers1

2

UITableView.automaticDimension is a CGFloat with a value of -1.0, so that's as intended. You can therefore not use UITableView.automaticDimension in calculations.

If each cell can have different heights, depending on the text length in each webview, my suggestion is that you add a height constraint to each cell (or to the webview in each cell), and then calculate the height of the text, setting the height constraint to this value.

To calculate the height of your webview, have a look at How to determine the content size of a WKWebView?

rodskagg
  • 3,827
  • 4
  • 27
  • 46