1

I'm working on autolayout for UITableViewCell with a multiline UILabel. My UILabel is anchored to top, right, bottom and left. Everything is working fine, until I add some top margin; if I do, some cell heights are calculated fine, and some don't, so the text gets truncated on those cases.

I've simplified my view as is a bit more complex. It looks like this:

enter image description here

enter image description here

This is the relevant code where I calculate the cell's height:

CellMessage *cell = self.prototypeCell;
cell.message = [self messageForRowAtIndexPath:indexPath];
[self.prototypeCell layoutIfNeeded];

CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    message.cellHeight = size.height + 1;

I have read lots of sof threads and tutorials, and all of them differ on subtle points, sometimes they said setting a preferred max width is the key point, I tried setting and not setting it, but I'm having the same results, still I don't completely understand how this param works :(

What am I missing / doing wrong? Anything I could try, or any tip pointing in the right direction would be much appreciated.

ssantos
  • 16,001
  • 7
  • 50
  • 70
  • have you tried to set trailing constraint to `=`? – Sega-Zero Jul 03 '15 at 19:00
  • http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout – GaétanZ Jul 03 '15 at 19:02
  • I wonder why do you set Trailling Space >=? If you want the cell to UILabel to expand, try using tralling space = and set compression resistance priority higher that the default (ie. 751) – thanyaj Jul 03 '15 at 19:02
  • Thanks for the comments .I set `>=` because I need the `UILabel` to shrink if the text is small, as the real view has a background balloon (think of whatsapp conversation view). Anyway, just tried `=` and I'm having the same issue. – ssantos Jul 03 '15 at 19:11
  • How about setting the compression resistance priority to be more than the Trailing Space priority? – thanyaj Jul 03 '15 at 19:23
  • Thanks @thanyaj. Just tried but didn't work neither :( – ssantos Jul 03 '15 at 19:27
  • read [this](http://stackoverflow.com/a/18746930/1254172) answer, it may help – Sega-Zero Jul 03 '15 at 20:27
  • Thanks @Sega-Zero, looks dense yet promising, I'll take a look :) – ssantos Jul 03 '15 at 20:36

1 Answers1

0

So, I had a similar problem, not long ago. I worked it out this way (it was on swift):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CellMessage *cell = (CellMessage *)[tableView.dataSource tableView:tableView cellForRowAtIndexPath:indexPath];
[cell sizeToFit];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

CGSize size = [cell systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];

return size.height+1;
}

This will calculate the size WITH the text data. But you will have one more problem, to calculate the text right, you will need to use a constrain to determine the width, and it can't be trailing/leading, it needs to be a width constrain, so on UITableViewCell custom class you can do:

- (void)updateConstraints {
[super updateConstraints];
self.myLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.myLabel.frame);
}

The titleLabel is a UILabel linked as property.

By the way, try to fix your trailing to a constant, not an >=

RodolfoAntonici
  • 1,555
  • 21
  • 34
  • Could you answer in the same language the OP programs in? He writes in ObjC and even though they are easily translatable, it would be nicer to have it in ObjC as well. – Julian F. Weinert Jul 03 '15 at 19:16
  • Thanks @RodolfoAntonici. It looked promising, but I'm still having the same issue :( I must be missing something obvious here. – ssantos Jul 03 '15 at 19:17
  • Thanks @Julian. As for me, after 12 hours struggling with it (really), it's ok whatever language as long as it works :) – ssantos Jul 03 '15 at 19:23
  • Yeah, you'r right, @ssantos. I have to admit, auto layout is a real bitch, especially when dealing with tableViews. I had the same struggle as well and don't use auto layout for this specific row anymore. – Julian F. Weinert Jul 03 '15 at 19:25
  • Just changed the language, hope it helps. Where do you set your label text? – RodolfoAntonici Jul 03 '15 at 19:26
  • Thanks @RodolfoAntonici. I'm setting label text here `cell.message = [self messageForRowAtIndexPath:indexPath];` in the `setMessage` method, just before the height calculating stuff, is that ok? – ssantos Jul 03 '15 at 19:29