OGeek|极客世界-中国程序员成长平台

标题: ios - 在 tableViewCell 中添加 Webview [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:14
标题: ios - 在 tableViewCell 中添加 Webview

我将 WebView 放在 tableViewCell 中,然后在 tableView 中使用它。到目前为止,一切正常。但我为 WebViewCell 使用了固定大小。现在我想显示 WebView 的所有内容。我在 MyTbaleView 类中将我的代码更改为这个

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

     if let cell = modelCollection[collection.identifier] {
            if let _ = cell as? TiteCell{
              return 200
             } else if let _ = cell as? myWebViewCell{
              return UITableViewAutomaticDimension
             }
     }



func tableView(tableView: UITableView, EstimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
     return 1000
}

这是xib文件

myWebView Xib file

使用这段代码,WebView 变得非常小,只有几毫米。我试图将我在论坛中找到的这段代码添加到 MyWebViewCell

func webViewDidFinishLoad(webView : UIWebView) {
        var frame = myWebView.frame
        frame.size.height = 1
        myWebView.frame = frame

        let fittingSize = myWebView.sizeThatFits(CGSize(width: 0, height: 0))
        frame.size = fittingSize
        myWebView.frame = frame
        myWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

现在webview大了一点,1厘米,宽度比ipad的宽度还大。



Best Answer-推荐答案


正如@Ramaraj T 所说,这是一个非常昂贵的过程,我同意你应该改变你的设计。但如果你还想继续,你可以这样做。

在您的自定义单元格中为 WebView 创建一个 HeightConstraint。

// Code in cell
var webViewHeightConstraint: NSLayoutConstarint!

// Code in controller 
// code cellForIndexPath
webView.tag = indexPath.row(or any unique value which can be used to calculate indexPath)

func webViewDidFinishLoad(webView : UIWebView) {
   var frame = myWebView.frame
   frame.size.height = 1
   myWebView.frame = frame

   let fittingSize = myWebView.sizeThatFits(CGSize(width: 0, height: 0))
   frame.size = fittingSize
   myWebView.frame = frame
   myWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

   let height = myWebView.contentSize.height
   let section = webView.tag
   let indexPath = NSIndexPath(forRow: 0, inSection: section)
   let cell = tableView.dequeCell(atIndexPath: indexPath)// 
   cell.webViewHeightConstraint.constant = height
   cell.layoutConstraintsIfNeeded()
   tableView.beginUpdates()
   tableView.endUpdates()
}

关于ios - 在 tableViewCell 中添加 Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929457/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4