0

I have a tableViewCell with a label inside that could be multiple lines tall. I've set the label's Lines property to 0. However, when I make the label's text have multiple lines the text gets cut off. Here's how I've set up my storyboard:

cell

Does anybody know how I made the table's cells just tall enough to contain the labels within?

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • put constrain hight greater then equal and use UITableViewAutomaticDimension for tableview rowhight – Himanshu Moradiya Apr 22 '17 at 07:13
  • Have you set `lineBreakMode`? – RajeshKumar R Apr 22 '17 at 07:13
  • You need to fully specify the height of the cell using constraints. For example: If the label basically defines the height of the cell then you should pin the top of the label to the top of the content view and the bottom of the label to the bottom of the content view. – Robotic Cat Apr 22 '17 at 07:16
  • Take ref from here : http://stackoverflow.com/questions/42416733/self-sizing-tableview-cells-based-on-two-subviews-height/42417325#42417325 – dahiya_boy Apr 22 '17 at 07:31

2 Answers2

3

Setting Dynamic Cell height procedure

  1. Pin the label from top and bottom. Please refer following Screen shot

enter image description here

  1. Set numbers of line to 0 of the label as from property inspector of xcode, it can be done from code too please refer following screen shot

enter image description here

  1. Implement delegates of table view mentioned below

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50 // also UITableViewAutomaticDimension can be used
    }
    
dip
  • 3,548
  • 3
  • 24
  • 36
0

You are missing the bottom constraint from the label to the table view cell (as far as I can tell). In order to make autolayout know how large the height of the cell has to be, you need to supply those constraints.

In addition do not forget to provide the estimatedRowHeight to the table view. If that value is not given, automatic cell sizing will not work.

mAu
  • 2,020
  • 1
  • 14
  • 27