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

前端 未结 4 1517
死守一世寂寞
死守一世寂寞 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] = ["<!DOCTYPE html><html><head><title>Page Title</title></head><body><h1>My First Heading</h1><p>My first paragraph</p></body></html>", "<HTML><HEAD><TITLE>Coca-Cola</TITLE></HEAD><BODY>In Chinese, Coca-Cola means Bite the Wax Tadpole</BODY></HTML>"]
        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)
        }
    }
    
    0 讨论(0)
  • 2020-12-25 13:40

    start an update of the tableview and then end it without changing anything.

    tableView.beginUpdates()
    tableView.endUpdates()
    

    This should make the tableview set the new height

    You can regulate the height by either implementing the (a)heightForRowAtIndexPath with logic setting the heights or (b)with auto layout and automatic tableview row height

    A.

    override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
         if [your condition, row == 5 in your comment] {
              return 100
         } else {
             return 40
         }
    }
    

    Whenever you want to change the height you would just call these two rows

    tableView.beginUpdates()
    tableView.endUpdates()
    

    B.

    in viewDidLoad

    tableView.estimatedRowHeight = 40.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

    and then you can just expose the layout constraint that set's the cells height and access it to set the height when you want

    cell.heightConstraint.constant = 100
    tableView.beginUpdates()
    tableView.endUpdates()
    
    0 讨论(0)
  • 2020-12-25 13:48

    You can use this code to update the cell's height without reloading their data:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

    UITableView Class Reference

    0 讨论(0)
  • 2020-12-25 13:53

    You can use the view height constraint in the cell, by updating the view in cell height you can update the specific cell height.

    let height = cell.Height_constraint.constant
    cell.Height_constraint.constant = height + 200 //200 you can use any number
    

    Height_constraint: is the height constraint of subView in my cell.

    0 讨论(0)
提交回复
热议问题