Dynamic Height Issue for UITableView Cells (Swift)

前端 未结 25 2533
温柔的废话
温柔的废话 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:52

    In my case - In storyboard i had a two labels as in image below, both labels was having desired width values been set before i made it equal. once you unselect, it will change to automatic, and as usual having below things should work like charm.

    1.rowHeight = UITableView.automaticDimension, and
    2.estimatedRowHeight = 100(In my case).
    3.make sure label number of lines is zero.
    

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

    In addition to what others have said,

    SET YOUR LABEL'S CONSTRAINTS RELATIVE TO THE SUPERVIEW!

    So instead of placing your label's constraints relative to other things around it, constrain it to the table view cell's content view.

    Then, make sure your label's height is set to more than or equal 0, and the number of lines is set to 0.

    Then in ViewDidLoad add:

    tableView.estimatedRowHeight = 695
    
    tableView.rowHeight = UITableViewAutomaticDimension
    
    0 讨论(0)
  • 2020-11-28 04:54

    Unfortunately, I am not sure what I was missing. The above methods don't work for me to get the xib cell's height or let the layoutifneeded()or UITableView.automaticDimension to do the height calculation. I've been searching and trying for 3 to 4 nights but could not find an answer. Some answers here or on another post did give me hints for the workaround though. It's a stupid method but it works. Just add all your cells into an Array. And then set the outlet of each of your height constraint in the xib storyboard. Finally, add them up in the heightForRowAt method. It's just straight forward if you are not familiar with the those APIs.

    Swift 4.2

    CustomCell.Swift

    @IBOutlet weak var textViewOneHeight: NSLayoutConstraint!
    @IBOutlet weak var textViewTwoHeight: NSLayoutConstraint!
    @IBOutlet weak var textViewThreeHeight: NSLayoutConstraint!
    
    @IBOutlet weak var textViewFourHeight: NSLayoutConstraint!
    @IBOutlet weak var textViewFiveHeight: NSLayoutConstraint!
    

    MyTableViewVC.Swift

    .
    .
    var myCustomCells:[CustomCell] = []
    .
    .
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("CustomCell", owner: self, options: nil)?.first as! CustomCell
    
    .
    .
    myCustomCells.append(cell)
    return cell
    
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
       let totalHeight = myCustomCells[indexPath.row].textViewOneHeight.constant + myCustomCells[indexPath.row].textViewTwoHeight.constant +  myCustomCells[indexPath.row].textViewThreeHeight.constant + myCustomCells[indexPath.row].textViewFourHeight.constant + myCustomCells[indexPath.row].textViewFiveHeight.constant
    
      return totalHeight + 40 //some magic number
    
    
    }
    
    0 讨论(0)
  • 2020-11-28 04:58

    Try

    override func viewWillAppear(animated: Bool) {
        self.tableView.layoutSubviews()
    }
    

    I had the same problem and it works for me.

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

    To make autoresizing of UITableViewCell to work make sure you are doing these changes :

    • In Storyboard your UITableView should only contain Dynamic Prototype Cells (It shouldn't use static cells) otherwise autoresizing won't work.
    • In Storyboard your UITableViewCell's UILabel has configured for all 4 constraints that is top, bottom, leading and trailing constraints.
    • In Storyboard your UITableViewCell's UILabel's number of lines should be 0
    • In your UIViewController's viewDidLoad function set below UITableView Properties :

      self.tableView.estimatedRowHeight = <minimum cell height> 
      self.tableView.rowHeight = UITableViewAutomaticDimension
      
    0 讨论(0)
  • 2020-11-28 05:00
    self.Itemtableview.estimatedRowHeight = 0;
    self.Itemtableview.estimatedSectionHeaderHeight = 0;
    self.Itemtableview.estimatedSectionFooterHeight = 0;
    
    
    [ self.Itemtableview reloadData];
    self.Itemtableview.frame = CGRectMake( self.Itemtableview.frame.origin.x,  self.Itemtableview.frame.origin.y,  self.Itemtableview.frame.size.width,self.Itemtableview.contentSize.height + self.Itemtableview.contentInset.bottom + self.Itemtableview.contentInset.top);
    
    0 讨论(0)
提交回复
热议问题