0

Edit: this is different from the duplicate because I have two variable-height labels, not one

I'm trying to do what I thought would be very simple. I have a table cell that has two labels in it. Each of those labels is loaded in via a dynamic data source and they could be of any length. I need the the labels to grow and shrink according to the data that's inside of them. This is turning out to be quite a challenge.

I have created a new project that is absolutely bare boned in hopes of getting to the root of the problem.

Scenario 1

Scenario 2

Both of my labels are set to 0 lines and line breaks: Word Wrap

Any help is great appreciated.

And the code that I currently have:

    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 5;
    }

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        return [self.tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    }

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

Solution found:

I added the following line of code before returning the cell, and everything magically worked.

[cell updateConstraintsIfNeeded];
Bryan Deemer
  • 743
  • 1
  • 6
  • 18
  • Do you have an implementation for `-tableView:heightForRowAtIndexPath:` defined? – fullofsquirrels May 17 '16 at 18:26
  • Have you tried setting estimatedRowHeight directly to you tableView and implement heightForRow method returning UITableViewAutomaticDimension value? – Alex Kosyakov May 17 '16 at 18:32
  • I have, and it yields the exact same results – Bryan Deemer May 17 '16 at 18:42
  • To paraphrase that answer in the dup, set the table view rowHeight = UITableViewAutomaticDimension, don't put a height constraint on the labels, just a chain of constraints from the tops and bottoms that end at the top and bottom of the cell. – danh May 17 '16 at 18:46
  • @BryanDeemer - same idea: if the labels are next to each other, make a chain of constraints for both, superview top to label top, label bottom to superview bottom. If the labels are stacked vertically, then the single chain is superview top to labelA top, labelA bottom to labelB top, labelB bottom to superview bottom. – danh May 17 '16 at 19:20
  • Try this and implement tableviewdelegate methid for heightCGflaot height= [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+2; – user3306145 May 18 '16 at 06:56

0 Answers0