1

I have a tableview that contains a custom cell. The custom cell has an imageview and a label. I want to resize the cell based on the text in the label because the imageview dimensions are constant. The imageview takes up the majority of the cell being centered and the label is below it.

self->tableView.rowHeight = UITableViewAutomaticDimension;

I am using this line of code to do so, and I have also set up constraints on the storyboard. When the code runs the image does not show, only the label and the cell is not even resized based off of the text. When I set the view to a default height, both are shown. I am unsure why this is not working. My only thought is that my constraints are incorrect. I have successfully used this with 2 labels but not an imageview. Any help would be appreciated. I would show a picture regarding my constraints, but I can not due to my status.

John55
  • 301
  • 2
  • 11

3 Answers3

0

If you have layout like this of your tableViewCell then set your constraints as shown in below.

You also need to set estimatedRowHeight of your tableView.

 self.tableView.estimatedRowHeight = 200;
 self.tableView.rowHeight = UITableViewAutomaticDimension;

Your image constraints

Your label's constraints

rushisangani
  • 3,195
  • 2
  • 14
  • 18
0

Remove height constraint form label if set and set label noOflines = 0. It will help you.

Dhaval Patel
  • 95
  • 12
0

Make you set the following

-label numberOfLines to 0

-don't set height for label

-in the bottom contraint between the label and the cell contentView, set it to greater than or equal to e.g 5

Then call the methods below

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

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
Johnson
  • 243
  • 3
  • 7