Dynamic Height Issue for UITableView Cells (Swift)

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

提交回复
热议问题