【问题标题】:Add a Webview in a tableViewCell在 tableViewCell 中添加 Webview
【发布时间】:2016-12-02 10:03:42
【问题描述】:

我将 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 class

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

【问题讨论】:

  • tableviewcell 中的 Web 视图?这是一个代价高昂的过程,您必须考虑重新设计。如果你只想要富文本,你可以在 UILabel 中使用 NSAttributedString。
  • 我只想从互联网上加载一个网页并在表格视图单元格中显示它
  • 您是否尝试过设置缩放页面以适应属性?
  • 我只是厌倦了添加 myWebView.scalesPageToFit = true 但没有帮助

标签: ios swift uitableview tableview


【解决方案1】:

正如@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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多