菜鸟教程小白 发表于 2022-12-11 18:14:19

ios - 在 tableViewCell 中添加 Webview


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

<pre><code>func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {

   if let cell = modelCollection {
            if let _ = cell as? TiteCell{
            return 200
             } else if let _ = cell as? myWebViewCell{
            return UITableViewAutomaticDimension
             }
   }



func tableView(tableView: UITableView, EstimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
   return 1000
}
</code></pre>

<p>这是xib文件</p>

<p> <a href="/image/LKjLx.png" rel="noreferrer noopener nofollow">myWebView Xib file</a> </p>

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

<pre><code>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);
}
</code></pre>

<p>现在webview大了一点,1厘米,宽度比ipad的宽度还大。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>正如@Ramaraj T 所说,这是一个非常昂贵的过程,我同意你应该改变你的设计。但如果你还想继续,你可以这样做。</p>

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

<pre><code>// 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()
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 tableViewCell 中添加 Webview,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40929457/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40929457/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 tableViewCell 中添加 Webview