Dynamic Height Issue for UITableView Cells (Swift)

前端 未结 25 2529
温柔的废话
温柔的废话 2020-11-28 04:39

Dynamic text of variable length are being injected into tableview cell labels. In order for the tableview cells\' heights to be dynamically sized, I have implemented in

相关标签:
25条回答
  • 2020-11-28 04:47

    Try This:

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    EDIT

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    Swift 4

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    Swift 4.2

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    

    Define above Both Methods.
    It solves the problem.

    PS: Top and bottom constraints is required for this to work.

    Here is example

    0 讨论(0)
  • 2020-11-28 04:48

    I was just inspired by your solution and tried another way.

    Please try to add tableView.reloadData() to viewDidAppear().

    This works for me.

    I think the things behind scrolling is "the same" as reloadData. When you scroll the screen, it's like calling reloadData() when viewDidAppear .

    If this works, plz reply this answer so I could be sure of this solution.

    0 讨论(0)
  • 2020-11-28 04:49

    Dynamic sizing cell of UITableView required 2 things

    1. Setting the the right constraint of your view inside the table view cell (mostly it includes giving your view proper top , bottom and traling constraints)
    2. Calling these properties of TableView in viewDidLoad()

       tableView.rowHeight = UITableViewAutomaticDimension
      
       tableView.estimatedRowHeight = 140
      

    This is a wonderfull tutorial on self-sizing (dynamic table view cells) written in swift 3 .

    0 讨论(0)
  • 2020-11-28 04:50

    Set automatic dimension for row height & estimated row height and ensure following steps:

    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Set automatic dimensions for row height
        // Swift 4.2 onwards
        table.rowHeight = UITableView.automaticDimension
        table.estimatedRowHeight = UITableView.automaticDimension
    
    
        // Swift 4.1 and below
        table.rowHeight = UITableViewAutomaticDimension
        table.estimatedRowHeight = UITableViewAutomaticDimension
    
    }
    
    
    
    // UITableViewAutomaticDimension calculates height of label contents/text
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // Swift 4.2 onwards
        return UITableView.automaticDimension
    
        // Swift 4.1 and below
        return UITableViewAutomaticDimension
    }
    

    For Example: if you have a label in your UITableviewCell then,

    • Set number of lines = 0 (& line break mode = truncate tail)
    • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
    • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

    Here is sample label with dynamic height constraints.

    enter image description here

    0 讨论(0)
  • 2020-11-28 04:51
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 88.0
    

    And don't forget to add botton constraints for label

    0 讨论(0)
  • 2020-11-28 04:52

    For Swift 3 you can use the following:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    0 讨论(0)
提交回复
热议问题