Я использовал WKWebView
внутри ячейки Tableview
, его height
не меняется в зависимости от содержимого внутри webView.
Если я непосредственно загружаю htmlString
внутри веб-просмотра, высота ячейки не меняется даже после вызова updateCellHeight()
.
Это моя пользовательская ячейка (urlString
исходит из ответа на вызов API, поэтому каждый раз он будет другим):
let cell1: NewCatDescrDCell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! NewCatDescrDCell
if !loaded {
cell1.urlString = "<p>Headers and Builders</p>"
cell1.configure()
cell1.tableView = tableView
}
loaded = true
return cell1
}
Это NewCatDescrDCell
ячейка:
class NewCatDescrDCell: UITableViewCell, WKNavigationDelegate {
let webView = WKWebView()
var tableView: UITableView!
var heights : CGFloat = 1200.0
var heightconstraint: NSLayoutConstraint!
var urlString: String = ""
override func awakeFromNib() {
super.awakeFromNib()
webView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(webView)
webView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
webView.allowsBackForwardNavigationGestures = true
}
func configure() {
webView.navigationDelegate = self
webView.scrollView.isScrollEnabled = true
webView.loadHTMLString(urlString, baseURL: nil)
}
@objc func updateCellHeight() {
heights = webView.scrollView.contentSize.height
heightconstraint = webView.heightAnchor.constraint(equalToConstant: heights)
heightconstraint.isActive = true
tableView.reloadData()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateCellHeight), userInfo: nil, repeats: false)
}
}
height
изменяется динамически, если я использую url
для загрузки внутри WebView
, но он не работает для HTMLString
.