<i id='cmvLl'><tr id='cmvLl'><dt id='cmvLl'><q id='cmvLl'><span id='cmvLl'><b id='cmvLl'><form id='cmvLl'><ins id='cmvLl'></ins><ul id='cmvLl'></ul><sub id='cmvLl'></sub></form><legend id='cmvLl'></legend><bdo id='cmvLl'><pre id='cmvLl'><center id='cmvLl'></center></pre></bdo></b><th id='cmvLl'></th></span></q></dt></tr></i><div id='cmvLl'><tfoot id='cmvLl'></tfoot><dl id='cmvLl'><fieldset id='cmvLl'></fieldset></dl></div>

    <small id='cmvLl'></small><noframes id='cmvLl'>

    <legend id='cmvLl'><style id='cmvLl'><dir id='cmvLl'><q id='cmvLl'></q></dir></style></legend>

      <tfoot id='cmvLl'></tfoot>
        • <bdo id='cmvLl'></bdo><ul id='cmvLl'></ul>
      1. UITableViewCell 与 UIWebView 动态高度

        时间:2023-10-22
        • <small id='V6jNV'></small><noframes id='V6jNV'>

          <tfoot id='V6jNV'></tfoot>
        • <legend id='V6jNV'><style id='V6jNV'><dir id='V6jNV'><q id='V6jNV'></q></dir></style></legend>

              1. <i id='V6jNV'><tr id='V6jNV'><dt id='V6jNV'><q id='V6jNV'><span id='V6jNV'><b id='V6jNV'><form id='V6jNV'><ins id='V6jNV'></ins><ul id='V6jNV'></ul><sub id='V6jNV'></sub></form><legend id='V6jNV'></legend><bdo id='V6jNV'><pre id='V6jNV'><center id='V6jNV'></center></pre></bdo></b><th id='V6jNV'></th></span></q></dt></tr></i><div id='V6jNV'><tfoot id='V6jNV'></tfoot><dl id='V6jNV'><fieldset id='V6jNV'></fieldset></dl></div>
                  <bdo id='V6jNV'></bdo><ul id='V6jNV'></ul>
                    <tbody id='V6jNV'></tbody>
                  本文介绍了UITableViewCell 与 UIWebView 动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个表格视图,其中包含有 webView 的单元格,我希望单元格的高度与 webView 的高度相匹配.

                  I have a table view with cells that have a webView in them, I want the height of the cell to match the height of the webView.

                  这是我使用的代码:

                  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                  
                      let cell = tableView.dequeueReusableCellWithIdentifier("newsCell") as! NewsTableViewCell
                  
                      cell.webView.loadHTMLString("test<br>test<br>test<br>test<br>test<br>test", baseURL: nil)
                      cell.webView.delegate = self
                  
                      var webFrame = cell.webView.frame
                      var cellFrame = cell.frame
                      cell.frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, cellFrame.width, webFrame.height + 20)
                      cell.backgroundColor = UIColor.redColor()
                  
                      return cell
                  }
                  
                  func webViewDidFinishLoad(webView: UIWebView) {
                      println("finished loading")
                      var frame = webView.frame
                      frame.size.height = 1
                      webView.frame = frame
                  
                      var fittingSize = webView.sizeThatFits(CGSizeZero)
                      frame.size = fittingSize
                      webView.frame = frame
                  
                      var height: CGFloat = frame.height
                      println(height)
                  
                      webView.frame = CGRectMake(frame.origin.x, frame.origin.x, frame.size.width, frame.size.height)
                  
                      newsTable.beginUpdates()
                      newsTable.endUpdates()
                  }    
                  

                  结果如下:https://postimg.cc/image/8qew1lqjj/

                  webView 是正确的高度,但单元格不是,我该如何解决这个问题?

                  The webView is the correct height but the cell isn't, How can I fix this problem?

                  推荐答案

                  TableView 会自己调整单元格大小,你只需要实现 tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->CGFloat 委托方法.

                  TableView will resize cells itself, you just need implement tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat delegate method.

                  是的,你最初并不知道WebView的高度,但是你可以计算出来然后让TableView重新加载cell.像这样的:

                  Yes, you don't know the height of WebView initially, but you can calculate it and then ask TableView to reload cell. Something like this:

                  class TableViewController: UITableViewController, UIWebViewDelegate
                  {
                      var content : [String] = ["test1<br>test1<br>test1<br>test1<br>test1<br>test1", "test22<br>test22<br>test22<br>test22<br>test22<br>test22"]
                      var contentHeights : [CGFloat] = [0.0, 0.0]
                  
                      // ...
                  
                      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
                      {
                          let cell = tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as! NewsTableViewCell
                          let htmlString = content[indexPath.row]
                          let htmlHeight = contentHeights[indexPath.row]
                  
                          cell.webView.tag = indexPath.row
                          cell.webView.delegate = self
                          cell.webView.loadHTMLString(htmlString, baseURL: nil)
                          cell.webView.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
                  
                          return cell
                      }
                  
                      override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
                      {
                          return contentHeights[indexPath.row]
                      }
                  
                      func webViewDidFinishLoad(webView: UIWebView)
                      {
                          if (contentHeights[webView.tag] != 0.0)
                          {
                              // we already know height, no need to reload cell
                              return
                          }
                  
                          contentHeights[webView.tag] = webView.scrollView.contentSize.height
                          tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
                      }
                  
                      // ...
                  }
                  

                  这篇关于UITableViewCell 与 UIWebView 动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:允许在 UIWebView 上禁用滚动? 下一篇:如何更改 WKWebView 或 UIWebView 默认字体

                  相关文章

                  <small id='IYqmw'></small><noframes id='IYqmw'>

                    <bdo id='IYqmw'></bdo><ul id='IYqmw'></ul>
                  <legend id='IYqmw'><style id='IYqmw'><dir id='IYqmw'><q id='IYqmw'></q></dir></style></legend>
                    <i id='IYqmw'><tr id='IYqmw'><dt id='IYqmw'><q id='IYqmw'><span id='IYqmw'><b id='IYqmw'><form id='IYqmw'><ins id='IYqmw'></ins><ul id='IYqmw'></ul><sub id='IYqmw'></sub></form><legend id='IYqmw'></legend><bdo id='IYqmw'><pre id='IYqmw'><center id='IYqmw'></center></pre></bdo></b><th id='IYqmw'></th></span></q></dt></tr></i><div id='IYqmw'><tfoot id='IYqmw'></tfoot><dl id='IYqmw'><fieldset id='IYqmw'></fieldset></dl></div>

                      <tfoot id='IYqmw'></tfoot>