我完全使用自动布局,你所说的有点问题。
我不想为了 UITable 的性能目的而修改内部大小的计算方式。
所以我用了一个很简单的方法最终是正确的。如果您的单元格很简单,那没关系,但如果您的单元格包含多个可变文本,就会变得很困难。
我正常定义了我的单元格,您可以在其中放置适合插图的 UILabel(没问题)。
然后,在表数据源中,直接定义单元格的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [TEXTOFYOURCELL sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 1000)].height + 31; // Here it's defined for 15 of top and bottom insets, define +1 than the size of the cell is important.
}
EDIT :这里有一些关于单元格中 UILabel 的代码(在 init 方法中)。
__titleLabel = [UILabel new];
__titleLabel.numberOfLines = 0;
[self.contentView addSubview:__titleLabel]; // adding to contentView rather than self is very important !
[__titleLabel keepInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
我使用这个API:https://github.com/iMartinKiss/KeepLayout https://github.com/iMartinKiss/KeepLayout更简单地管理自动布局。