UITableViewCell With UIWebView Dynamic Height

前端 未结 10 2048
深忆病人
深忆病人 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:11

    It's work for me, easy Way to load Html String on WebView with dynamic.

    First take textView in TableViewCell with scroll disable and then take WebView, Apply Zero constraint on all 4 Side with ContentView. Now give same height constraint to both textview and WebView.Now give same text to both view as shown below code.

    For Clean UI Hide textView From XIB or storyBoard.

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewcell") as! tableviewcell
        let theString : String = "

    Subheader

    " let theAttributedString = try! NSAttributedString(data: theString.data(using: String.Encoding.utf8, allowLossyConversion: false)!,options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) cell.textview.attributedText = theAttributedString cell.webview.loadHTMLString(theString, baseURL: nil) return cell }

提交回复
热议问题