0

My app has a table view with automatic sized row heights (UITableViewAutomaticDimension for heightForRow-Delegate-Function). My custom cells have subviews, that can have different sizes (they contain an always different number of bar charts).

However, automatic cell height does not work to fit these different sizes of the subview of the tableview cell.

It only works, if I set a constraint (e.g. make.top.equalTo(self).offset(300) with snap kit) for the subview in the tableview cell - but since these subviews can have different sizes, this is not a clean solution.

Do you know a better approach?

Thanks!

Pascal
  • 2,590
  • 3
  • 21
  • 46
  • 1
    @whoever voted down this question: of course you can vote this down, however, I would recommend leaving a comment to lmk what you don’t like about this question - e.g. a link to a familiar question (which I did not find) or a hint if you don’t understand this question. – Pascal Nov 07 '17 at 16:57
  • Agree, if u will downvote something at least leave a comment. – alegelos Nov 07 '17 at 17:12

1 Answers1

1

First: on viewdidLoad (not in delegate):

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140 //this estimation will improve performance (can be omitted), set the +/- size of most cases.

Just don't override the cell height delegate.

Second: In your cell, u added that subView that contains chars. Add that subView constraints, but more important add a vertical spacing between that subview and the cell (this case the super view). Like the image.

enter image description here

If the subview height will set the cell height. Just make sure your chars inside the subView to set the subView height (with constraints).

For more details, here there's a good step by step.

alegelos
  • 2,308
  • 17
  • 26