2016-12-02 41 views
2

我把一個WebView放在一個tableViewCell中,我在tableView中使用它。到現在爲止一切工作正常。但是我爲WebViewCell使用了一個固定的大小。現在我想顯示WebView的所有內容。我改變了我的代碼,這一個在MyTbaleView在tableViewCell中添加一個Webview

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 
} 

,這是廈門國際銀行文件

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寬度。

+2

Webview在tableviewcell?這是一個昂貴的過程,你必須考慮重新設計。如果你只想要富文本,你可以在UILabel中使用NSAttributedString。 –

+0

我只是想從網上加載一個網頁,並在表格視圖單元格中顯示 – ella23

+0

您是否試圖通過設置比例頁來適應屬性? – Sreekanth

回答

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