3

I am trying to calculate the height of a table view cell that is layout using auto layout. As this has to be compatible to iOS7 as well, I need to return a valid height for heightForRowAtIndexPath:.

What I am doing is the following:

if (self.userRatingsAbbreviatedTextCell == nil) {
    self.userRatingsAbbreviatedTextCell = [[UserRatingsAbbreviatedTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
[self.userRatingsAbbreviatedTextCell prepareForReuse];
UserRating *rating = [self.paginationModel ratingAtIndex:reviewIndex];
[self.userRatingsAbbreviatedTextCell configureCellForHotelUserRating:rating];
[self.userRatingsAbbreviatedTextCell layoutIfNeeded];

CGSize size = [self.userRatingsAbbreviatedTextCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;

The cell has a disclosure indicator. When I ask the cell for its contentView width it is 296. However in cellForRowAtIndexPath: after dequeuing a cell, its contentView width is 289, which is the size that is used for rendering. Has anybody experienced such issues? What do I need to do for the cell to set its contentView width correctly?

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • if the table is static you can get height from `super` – meda Oct 30 '14 at 13:36
  • yet it is still broken... I have investigated this a bit more and have found that calling `_configureCellForDisplay:forIndexPath:` on the table view and passing it the cell resizes the content view to the correct size, yet as this is private API, that is not a fix for the issue... – Michael Ochs Oct 30 '14 at 15:46
  • Have you already seen this? http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – eofster Oct 30 '14 at 19:08
  • Yes, this is the way we do our cell layouting, but the iOS7 compatible way is not working on iOS8 anymore as soon as there is a disclosure indicator or similar accessory view. – Michael Ochs Nov 04 '14 at 10:00
  • I don't get your answer, what is broken exactly and what are the results of view debugging ? – A-Live Nov 04 '14 at 11:14
  • The results are the same that I posted in my original question: The size of the `contentView` is wrong when using a cell that has not been configured by the table view. The width is 296 instead of 289, which is the size when the cell is drawn by the table view. – Michael Ochs Nov 04 '14 at 12:31
  • I'd be interested in testing a small sample project, do you think it would be possible for you to prepare and share it ? – A-Live Nov 04 '14 at 12:44
  • @A-Live I filed this as rdar and attached the following sample project: https://www.dropbox.com/s/q367d5q6lfncf32/HeightCalculation.zip?dl=0 – Michael Ochs Dec 08 '14 at 12:14

1 Answers1

0

My solution to the contentView being a different size than the UITableViewCell. May break some of UITableViewCell's innate properties such as accessory views.

Override UITableViewCell's layoutSubviews method in your cell class:

- (void)layoutSubviews
{
    [super layoutSubviews];

    // force `contentView` to be the same size as `self` (it's typically 0.5pt smaller)
    CGRect contentViewFrame = self.contentView.frame;
    contentViewFrame.size = CGSizeMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.contentView.frame = contentViewFrame;
}

In my case, the contentView was always 0.5 points smaller and it was causing problems when calculating the correct height for tableView:heightForRowAtIndexPath:. I would calculate the perfect height for the cell that wouldn't cause a UILabel to wrap but it would always cut off the last line since the height was 0.5 points too small.

My cell, for reference:

Custom UITableViewCell XIB

Matt Robinson
  • 799
  • 7
  • 22