不使用固定值计算单元格高度

[英]Calculate Cell Height without Using Fixed Values


I need to calculate the height for a UITableViewCell with includes multiple rows of wrapping text. So far I couldn't find a 100% exact way to do this. Furthermore the technique I currently use relies heavily on fixed values:

我需要计算包含多行包装文本的UITableViewCell的高度。到目前为止,我找不到100%准确的方法来做到这一点。此外,我目前使用的技术在很大程度上依赖于固定值:

NSString *cellText;    
cellText = @"Very long multi line text in this String ...";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 32;

Fixed values used in just in these few lines:

仅在以下几行中使用的固定值:

  • Fontsize (14)
  • Font Type (Helvetica)
  • 字体类型(Helvetica)

  • CGSize, Width (280.0f)
  • CGSize,宽度(280.0f)

  • Random padding value that I found is needed (32)
  • 我发现需要随机填充值(32)

Is there a nice way to rewrite this in a way that all these fixed values are fetched and calculated dynamically?

有没有一种很好的方法可以通过动态获取和计算所有这些固定值的方式重写它?

That would help a lot to make the layout more responsive and the code reusable going forward. I appreciate any best practices.

这将有助于使布局更具响应性并且代码可以重复使用。我很欣赏任何最佳实践。

1 个解决方案

#1


1  

Your code seems good but u missed is cell appropriate calculation.U need to add label's topmost postion and label's bottommost postion in cell(parentView).

你的代码似乎很好,但你错过了细胞适当的计算。我需要在单元格(parentView)中添加标签的最顶层位置和标签的最底部位置。

Just calculate like this

就像这样计算

 return labelSize.height + 2*yourLabelInCell.frame.origin.y  //here label's topmost postion and label's bottommost postion in cell added

Label will adjust appropriately in cell.

标签将在单元格中适当调整。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/10/21/72021058f8d0d52575a03e2441b61821.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告