在TableViewCell中添加WebView



我将WebView放在tableViewCell中,然后在表观视频中使用它。到目前为止,一切都很好。但是我正在为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文件

使用此代码,网络浏览量变得很小,几毫米。我尝试将我在论坛中发现的代码添加到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宽度。

正如@ramaraj t所说,这是一个非常昂贵的过程,我批准您应该更改设计。但是,如果您仍然想继续,您可以做这样的事情。

在您的自定义单元格中为WebView创建一个高度标准。

// 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()
}

最新更新