Swift: How to reload row height in UITableViewCell without reloading data

前端 未结 4 1519
死守一世寂寞
死守一世寂寞 2020-12-25 13:20

I have a case in which I have to reload only height of a UITableViewCell.

but if I call the function

tableView.reloadRowsAtIndexPaths([         


        
4条回答
  •  庸人自扰
    2020-12-25 13:37

    About your question for example, to give a specific height dimension :

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      if indexPath.row == 3 {
        return 50.0
      }
    
      return 72.0
    }
    

    But I think you have a webView inside a cell so, generally, to calculate the dynamic height of a UITableViewCell with a UIWebView: (this example have two webViews)

    class MyTableViewController: UITableViewController, UIWebViewDelegate
    {
        var content : [String] = ["Page Title

    My First Heading

    My first paragraph

    ", "Coca-ColaIn Chinese, Coca-Cola means Bite the Wax Tadpole"] var contentHeights : [CGFloat] = [0.0, 0.0] override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCell", forIndexPath: indexPath) as! MyCustomCell 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) { // height knowed, no need to reload cell return } contentHeights[webView.tag] = webView.scrollView.contentSize.height tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic) } }

提交回复
热议问题