UITableViewCell With UIWebView Dynamic Height

前端 未结 10 2046
深忆病人
深忆病人 2020-12-04 11:52

I have a table view with cells that have a webView in them, I want the height of the cell to match the height of the webView.

This is the code I use:



        
10条回答
  •  北海茫月
    2020-12-04 11:58

    var heightOfWebview=0    
    
    func webViewDidFinishLoad(_ aWebView: UIWebView)
    {
        var frame: CGRect = aWebView.frame
        frame.size.height = 1
        aWebView.frame = frame
        let fittingSize = aWebView.sizeThatFits(CGSize.zero)
        frame.size = fittingSize
        aWebView.frame = frame
        heightOfWebview = Int(fittingSize.height)
        tableView.beginUpdates()
        tableView.endUpdates()
        print("Calling webViewDidFinishLoad. Cell size value: \(heightOfWebview)")
    
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return CGFloat(220+heightOfWebview) /// 220 is my static value
    }
    

提交回复
热议问题