2016-12-02 10 views
2

WebViewをtableViewCellに配置し、tableViewで使用します。これまではすべてがうまくいきました。しかし、私はWebViewCellに固定サイズを使用していました。今、WebViewのすべてのコンテンツを表示したいと思います。私は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 
} 

MyTbaleViewクラスでこの1に私のコードを変更し、これは、このコードのWebViewが本当に小さくなり、数ミリメートルで

myWebView Xib file

XIBファイルです。私は今のWebViewは少し大きめ1センチメートルで、幅はiPadの幅よりも大きくなっている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); 
} 

に私はフォーラムで見つかったこのコードを追加しようとしました。

+2

Webview in a tableviewcell?それはコストのかかるプロセスです。再設計について考える必要があります。リッチテキストが必要な場合は、NSAttributedStringをUILabelで使用できます。 –

+0

インターネットからWebページを読み込んでテーブルビューのセルに表示したいだけです – 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() 
} 
関連する問題