使用UITextView在UITableViewCell中自动布局

时间:2014-11-13 19:48:35

标签: ios uitableview autolayout

我正在尝试正确调整UITableViewCell的大小以适应UILabel和UITextView,以便标签和textView中的所有文本都可见。

现在,我无法获得textView的相应大小。这是我的布局代码:

    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;

现在,我已经确定我的笔尖有限制固定单元格内容的顶部,向下穿过标签,最后到单元格的底部。另外,单元也具有沿水平尺寸连接的约束。但是,如果我打印出这个尺寸,我会得到以下结果:

(width=486.5, height=61.5)

这很奇怪,因为如果我打印出textView和单元格,我会得到这个:

(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}>

因此textView看起来是正确的宽度,但由于某种原因,不会根据约束来计算它的高度。

这是我的layoutSubviews方法:

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

这是我的限制条件:

constraints on my cell

为什么我从-systemLayoutFittingSize:得到这个时髦的宽度?

更新

我认为我错误地在UITextView上配置了约束(当然,这是一个滚动视图),因为scrollview的intrinsicContentSize报告为(width=456.5, height=32.5)

1 个答案:

答案 0 :(得分:0)

我认为您需要将标签和文本视图上的内容压缩阻力设置为1000(必需)。正在发生的事情是标签和文字视图正在压缩它的内容。

您可能还需要在文本视图上创建高度约束,并将其设置为文本视图的contentSize。