在tableViewCell

时间:2016-12-02 10:03:43

标签: ios swift uitableview tableview

我将一个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宽度。

1 个答案:

答案 0 :(得分:0)

正如@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()
}
相关问题