16

I have a bit of a tricky set up in my storyboard, I have a UIViewController that holds a UITableViewController. Within the UITableViewController I have several prototypecells I have linked to subclassed uitableviewcell objects in code.

Using constraints and storyboard I would like to change the height of my prototype cell depending on the size the UILabel ends up being which is dependant on the text going into it.

Currently I have a

UIViewController
-- UITableViewController
-- -- UITableViewCell ((melchat) prototype cell)
-- -- -- ContentView
-- -- -- -- UIView ((background) view with drop shadow card type effect)
-- -- -- -- -- UIImageView (Avatar)
-- -- -- -- -- IUlabel (dynamic (depending on code/input) multi line UILabel)

Some how I would like the UILabel to resize the UIView (background) then in turn effect the height of that UITableViewCell.

I am using XCode 8.2.1

I have taken a screen shot of the layout in storyboard and constraints applied.

enter image description here

Update

I have updated my constraints to pretty much all go back to ContentView and have updated uilabel line count to 0 and then also implemented the UITableViewAutomaticDimension code but its still not working. Please see code and screen shots below.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;

}

enter image description here

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

5 Answers5

30

To go a little further on Dao's answer..

He is correct, you need to use UITableViewAutomaticDimension

Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as

  • Leading constraint to ImageView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView

Make sure that you set the UILabel to multiline (or lines = 0) and it should work.

If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • I have updated my question I might not have understood your answer.. as it didnt quite work. thnks for your response. – HurkNburkS Mar 10 '17 at 12:16
  • your constraint for the label to uiimagview should be label.leading = imageview.trailing + 10 (or whatever distance you want). currently it looks in the screenshot like there is still a height set. or is that the old screenshot? – Scriptable Mar 10 '17 at 12:24
  • I am still working on it. cannot get it to work for some reason. – HurkNburkS Mar 10 '17 at 21:03
  • Okay I kind of have it working.. my only issue now is that my image height is massive. so im going to fix that lol – HurkNburkS Mar 10 '17 at 21:09
  • yeah it just looks like the constraints need adjusting, if you can put your project on github I can take a look if your still struggling – Scriptable Mar 10 '17 at 21:12
13

Use UITableViewAutomaticDimension

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
Đào Minh Hạt
  • 2,742
  • 16
  • 20
9

step1:Give below constraint to UIView that is inside to the contentView.

  • Leading constraint to contentView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView

step2: Give below constraint to UIlabel

  • Leading constraint to UIImageView
  • Top constraint to UIView
  • Bottom constraint to UIView
  • Trailing constraint to UIView

step3: Then select your Trailing constraint of IUlabel and select edit option and then select constant and select greaterThanEqual option.

step4: set label's numberofline = 0

step5: Add this code into viewDidLoad()

yourtableview.estimatedRowHeight = 80.0
yourtableview.rowHeight = UITableViewAutomaticDimension
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
7

Swift 4.2 and 5. As mention above you can set UITableView object property or

tblView.rowHeight = UITableView.automaticDimension 
tblView.estimatedRowHeight = 300

you can also define the same by implementing UITableViewDelegate methods

extension ViewController: UITableViewDelegate {
      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
      }
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
      }
    }
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58
-1

After populating the UILabel and applying constrains, get the height of the label and set it as the height of the UIView.

After that call -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and pass the height of the UILabel/UIView so that it gets the actual height for that row

Ricardo Alves
  • 642
  • 2
  • 13
  • 34