好的,这就是我的困境,我有一段代码效果很好,它已被弃用,但它会计算任何文本块高度的适当大小。(以下作品但已弃用)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 2) {
CGSize size = [[item desc] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290 - (10 * 2), 200000.0f)];
CGFloat height = MAX(size.height, 44.0f);
return height + (2 * 2);
}
return 44;
}
试图删除不推荐使用的代码,所以我已经坚持了几天的正确方法来修复它,我想出了这个。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 2) {
NSStringDrawingContext *ctx = [NSStringDrawingContext new];
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[item desc]];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];
CGFloat height = MAX(textRect.size.height, 44.0f);
return height + (2 * 2);
}
return 44;
}
从代码级别的角度来看,这当然行不通,但它不能适当地调整单元格的大小。我觉得这应该很容易得到任何帮助。