UITableViewCell With UIWebView Dynamic Height

前端 未结 10 2079
深忆病人
深忆病人 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 12:17

    class WebViewTableViewCell: UITableViewCell {
        weak var viewController: ViewController? = nil
        @IBOutlet weak var webView: UIWebView!
        @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            webView.loadRequest(URLRequest(url: URL(string: "https://tinhte.vn")!))
            webView.delegate = self
            webView.scrollView.isScrollEnabled = false
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
    }
    extension WebViewTableViewCell: UIWebViewDelegate {
        func webViewDidFinishLoad(_ webView: UIWebView) {
            heightLayoutConstraint.constant = webView.scrollView.contentSize.height
            viewController?.tableView.beginUpdates()
            viewController?.tableView.endUpdates()
        }
    }

提交回复
热议问题