【发布时间】:2015-05-19 08:05:02
【问题描述】:
我遇到了一些与动态大小的单元格、自动布局和大小类有关的奇怪问题。我的测试项目完全基于 Ray 的教程。似乎唯一的区别是 UILabel 的大小类的字体大小。
当我在某个大小类中为标签设置另一个字体大小时,高度计算是错误的。我做了截图来说明它。
单元格的高度计算错误:
使用正确的单元格高度计算:
另外,我已经将test project推送到了github。
已编辑:
ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self heightForCellAtIndexPath:indexPath];
}
- (CGFloat)heightForCellAtIndexPath:(NSIndexPath *)indexPath {
static CustomTableViewCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:kBasicCell];
});
[self configurateCell:sizingCell atIndexPath:indexPath];
return [self calculateHeightForCell:sizingCell];
}
- (CGFloat)calculateHeightForCell:(CustomTableViewCell *)cell {
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height +1.0f;
}
CustomLabel.m
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
【问题讨论】:
-
仅供参考:给定的 Web 链接在我的国家/地区已被阻止。
-
在此处粘贴代码部分和图片,而不是链接和链接说明。
-
@refdev 感谢您的建议。我已经编辑了我的问题(添加代码部分)。不幸的是,由于 stackoverflow 的信誉系统,我无法直接粘贴图像。
-
@refdev 感谢编辑!
标签: ios objective-c autolayout size-classes