[已解决]我的错误......细节是UILabel而不是UITextView . 并且sizeToFit不会调整UILabel的大小 . 用UITextView替换UILabel解决了我的问题 .


我有一个固定高度的自定义单元格,其中所有约束都是垂直居中的视图(称为“内容”),其中包含 Headers 和说明 . 内容视图层次结构是:

  • 图标

  • Headers

  • 内容UIView

  • Headers UILabel

  • 详细信息UITextView

  • 页脚

当我在界面构建器中测试“详细信息”的值时,一切在预览(xCode 6)上都能正常工作:调整细节大小以包含文本,“内容”垂直居中 .

但是,当按代码设置'details'的内容并调用 sizeToFit 时,UITextView会正确调整大小,但不会更新约束 . 请参阅下面的 cellForRowAtIndexPath 代码 . 我错过了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // get a cell
    QZSetupPartyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PackCell" forIndexPath:indexPath];

    // fill the current cell with the current pack
    QZPack *pack = self.party.packs[indexPath.row];

    // set pack title
    cell.title.text = pack.json[@"title"];

    // resize textview to its content
    cell.details.text = pack.json[@"desc"];
    [cell.details sizeToFit];

    // async load of the icon
    [pack loadIconWithCompletion:^(UIImage *image) {
        cell.icon.image = image;
        [cell setNeedsLayout];
    }];

    // and recompute the cell constraints
    [cell layoutIfNeeded];
    [cell setNeedsDisplay];

    return cell;
}