UITableViewCell 复杂时,高度计算优

来源:互联网 发布:最新淘宝小模特征集 编辑:程序博客网 时间:2024/04/27 20:27

方法1:简单粗暴,利用新的api接口 systemLayoutSizeFittingSize

          

我们声明一个存计算Cell高度的实例变量:
  1. @property (nonatomic, strong) UITableViewCell *prototypeCell; 
然后初始化它:
  1. self.prototypeCell  = [self.tableView dequeueReusableCellWithIdentifier:@"C1"  ];

实现tableView的高度代理

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
  2.     C1 *cell = (C1 *)self.prototypeCell; 
  3.     cell.t.text = [self.tableData objectAtIndex:indexPath.row]; 
  4.     CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
  5.     CGSize textViewSize = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)]; 
  6.     CGFloat h = size.height + textViewSize.height; 
  7.     h = h > 89 ? h : 89;  //89是图片显示的最低高度, 见xib 
  8.     return 1  + h; 
要求返回一个Cell的估计值,

(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath。

方法优化1:cell使用属性保存,调用方便。并且防止计算cell高度时重复创建cell实例。
优化2,cell的高度通过新api计算cell.contentView自动适配后的的高度然后加1。
优化3,避免反复调用多次heightForRowAtIndexPath
优化4,sizeThatFits优化计算uitextview的高度
主要应用:Auto Layout with UILabel UITextView in UITableViewCell


方法2:灵活巧用CGSIZE
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  2.     C3 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C3"]; 
  3.     cell.t.text = [self.tableData objectAtIndex:indexPath.row]; 
  4.     [cell.t sizeToFit]; 
  5.     return cell; 
  6.   
  7. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
  8.     C3 *cell = (C3 *)self.prototypeCell; 
  9.     NSString *str = [self.tableData objectAtIndex:indexPath.row]; 
  10.     cell.t.text = str; 
  11.     CGSize s = [str calculateSize:CGSizeMake(cell.t.frame.size.width, FLT_MAX) font:cell.t.font]; 
  12.     CGFloat defaultHeight = cell.contentView.frame.size.height; 
  13.     CGFloat height = s.height > defaultHeight ? s.height : defaultHeight; 
  14.     NSLog(@"h=%f", height); 
  15.     return 1  + height; 

这儿用到了一个NSString的Cagetory方法:
  1. - (CGSize)calculateSize:(CGSize)size font:(UIFont *)font { 
  2.     CGSize expectedLabelSize = CGSizeZero; 
  3.      
  4.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
  5.         NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
  6.         paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; 
  7.         NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy}; 
  8.          
  9.         expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; 
  10.     } 
  11.     else { 
  12.         expectedLabelSize = [self sizeWithFont:font 
  13.                                        constrainedToSize:size 
  14.                                            lineBreakMode:NSLineBreakByWordWrapping]; 
  15.     } 
  16.   
  17.     return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height)); 


 使用到的方法  sizeToFit,boundingRectWithSize,sizeWithFont

          

0 0
原创粉丝点击