swift - TableView Cell 的高度不会根据 WebView 动态变化

标签 swift uitableview wkwebview

我在Tableview单元格中使用了WKWebView,它的高度不会根据webView内的内容而改变。

如果我直接在 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

     }

这是NewCatDecrDCell单元格:

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

}

如果我使用urlWebView内加载,height会动态变化,但它不适用于HTMLString.

最佳答案

我在 Stackoverflow 上发现了与 WKWebView 高度相关的类似问题。

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
    if complete != nil {
        self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
            self.containerHeight.constant = height as! CGFloat
        })
    }

    })}

试试这个或者在 Stackoverflow 上引用这个 How to determine the content size of a WKWebView?

关于swift - TableView Cell 的高度不会根据 WebView 动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55099415/

相关文章:

ios - OneSignal iOS SDK 发送通知失败代码 400

ios - 升级到 iOS8 后,UITableView didSelectRowAtIndexPath 不工作

ios - 将选定的 UITableViewCell 带回原来的位置

ios - 在 Swift 中引用一个按钮

Swift - 大中央调度类

ios - 如何在 Swift 中创建一个 UIPickerView 选择,以便 View 扩展以允许添加选项?

ios - 快速加载 UITableViewCell 中的 MKMapView

ios - 在 UIView 中创建 WKWebView

ios - 将 Javascript 添加到 WKWebView

objective-c - 将WKWebView嵌入YouTube