1

I have a custom table view cell that I want to automatically resize based on the amount of text in a label.

Here is my cell. I want to resize it in order to show all the text in the review label. enter image description here

In my table view controller I use UITableViewAutomaticDimension

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                return 200
            } else {
                return 49
            }
        } else if indexPath.section == 1 {
            return 100
        } else if indexPath.section == 2 {
            if indexPath.row == 0 {
                return 200
            } else {
                return 400
            }
        } else {
            return UITableViewAutomaticDimension
        }
    }

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

This is what I end up with.

enter image description here

raginggoat
  • 3,570
  • 10
  • 48
  • 108
  • Make sure you have the trailing, leading, top and bottom of the contentView connected to it's child views. – genaks Jun 08 '16 at 14:21
  • Have you checked out this article? http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Dominic Jun 08 '16 at 14:22
  • Do something like [link this](http://useyourloaf.com/blog/table-view-cells-with-varying-row-heights/), it will help you – Nirav D Jun 08 '16 at 14:23

3 Answers3

1

You need:

  1. set for your label top, trailing, leading, bottom constraints to superView in IB
  2. set numberOfLines = 0
  3. set Content Compression Resistance Priority Vertical and Content Hugging Priority Vertical
  4. set in code self.tableView.estimatedRowHeight = MinControlSize; self.tableView.rowHeight = UITableViewAutomaticDimension;

And after set the text call this

cell.setNeedsDisplay();
cell.layoutIfNeeded();

in delegate method

optional func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

Hope this help. I took this from Self Sizing example from Apple

iSashok
  • 2,326
  • 13
  • 21
0

Make sure you have constraints set for the top, bottom, trailing and leading margins to the contentView from a child UIView or whatever child elements you have. This might help you

enter image description here

In your case, you will need to define a bottom spacing constraint from the ReviewLabel to the contentView to make it work. The top, trailing and leading margins will be needed too from the corresponding UILabels. The top space can be defined from the DateLabel while the leading and trailing spaces can be defined from the UIView containing the stars

Why?

The contentView needs to know about the frame of it's child views to have the uitableviewautomaticdimension work on it. If it does not know about it's child view frames then it wouldn't know what to automatically resize to

genaks
  • 757
  • 2
  • 10
  • 24
0

Implement the height calculations in the delegate function:

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   let cell = tableView.cellForRowAtIndexPath(indexPath)
   let size = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
   return size.height + 1.0 // Add 1.0 for the cell separator height
}
user212514
  • 3,110
  • 1
  • 15
  • 11