Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An important enough gotcha I just ran into to post as an answer.</p> <p>@smileyborg's answer is mostly correct. However, if you have any code in the <code>layoutSubviews</code> method of your custom cell class, for instance setting the <code>preferredMaxLayoutWidth</code>, then it won't be run with this code:</p> <pre><code>[cell.contentView setNeedsLayout]; [cell.contentView layoutIfNeeded]; </code></pre> <p>It confounded me for awhile. Then I realized it's because those are only triggering layoutSubviews on the <code>contentView</code>, not the cell itself.</p> <p>My working code looks like this:</p> <pre><code>TCAnswerDetailAppSummaryCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TCAnswerDetailAppSummaryCell"]; [cell configureWithThirdPartyObject:self.app]; [cell layoutIfNeeded]; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return height; </code></pre> <p>Note that if you are creating a new cell, I'm pretty sure you don't need to call <code>setNeedsLayout</code> as it should already be set. In cases where you save a reference to a cell, you should probably call it. Either way it shouldn't hurt anything.</p> <p>Another tip if you are using cell subclasses where you are setting things like <code>preferredMaxLayoutWidth</code>. As @smileyborg mentions, "your table view cell hasn't yet had its width fixed to the table view's width". This is true, and trouble if you are doing your work in your subclass and not in the view controller. However you can simply set the cell frame at this point using the table width:</p> <p>For instance in the calculation for height:</p> <pre><code>self.summaryCell = [self.tableView dequeueReusableCellWithIdentifier:@"TCAnswerDetailDefaultSummaryCell"]; CGRect oldFrame = self.summaryCell.frame; self.summaryCell.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, self.tableView.frame.size.width, oldFrame.size.height); </code></pre> <p>(I happen to cache this particular cell for re-use, but that's irrelevant).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload