0
votes

I am trying to correctly size my UITableViewCell to fit a UILabel and a UITextView such that all text within the label and the textView is visible.

Now, I'm having trouble getting the appropriate size for the textView. Here is my layout code:

    if (_prototypeCell == nil) {
        _prototypeCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DDDataDetectingDetailCell class]) owner:nil options:0] lastObject];
    }

    [_prototypeCell setFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 1)];
    [_prototypeCell configureForItem:self.items[indexPath.row]];
    [_prototypeCell setNeedsLayout];
    [_prototypeCell layoutIfNeeded];

    CGSize size = [_prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;

Now, I've made sure that my nib has constraints pinning the top of the cell's contentView, down through the labels and finally to the bottom of the cell. Additionally, the cell has constraints connecting along the horizontal dimension too. However, if I print out this size, I get the following:

(width=486.5, height=61.5)

This is odd because if I print out the textView and the cell, I get this:

(lldb) po _prototypeCell
<DDDataDetectingDetailCell: 0x79bd4700; baseClass = UITableViewCell; frame = (0 0; 320 1); autoresize = RM+BM; layer = <CALayer: 0x79bbba90>>

(lldb) po _prototypeCell.detailTextView
<UITextView: 0x7b9ffc00; frame = (15 1; 290 0); text = 'No need to pay -- Just le...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x79bce590>; layer = <CALayer: 0x79bd6910>; contentOffset: {0, 0}; contentSize: {290, 0}>

So the textView appears to be the correct width, but for some reason is not calculating its height according to the constraints.

Here is my layoutSubviews method:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.mainLabel.preferredMaxLayoutWidth = self.mainLabel.frame.size.width;
    [super layoutSubviews];
}

And here is a pic of my constraints:

constraints on my cell

why am I getting this funky width from -systemLayoutFittingSize: ?

UPDATE

I think I am incorrectly configuring the constraints on the UITextView (which is, of course, a scroll view) since the intrinsicContentSize of the scrollview reports back as (width=456.5, height=32.5)

1
Did you set self.textView.scrollEnabled = NO; for your UITextView inside your cell class ?Zhang
yes i did this, unfortunately, it didnt work.Sean Danzeiser
Did you do it using interface builder ?Zhang

1 Answers

0
votes

I believe you need to set the content compression resistance on the label and text view to 1000 (required). what is happening is the label and text view are compressing it's content.

You may also need to create a height constraint on the text view and set it to be the contentSize of the text view.