[ios]UITableViewCell自适应高度 【转】

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 列寬
    CGFloat contentWidth = self.tableView.frame.size.width;
    // 用何種字體進行顯示
    UIFont *font = [UIFont systemFontOfSize:13];
    
    // 該行要顯示的內容
    NSString *content = [data objectAtIndex:indexPath.row];
    // 計算出顯示完內容需要的最小尺寸
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
    
    // 這裏返回需要的高度
    return size.height; 
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    // 列寬
    CGFloat contentWidth = self.tableView.frame.size.width;
    // 用何種字體進行顯示
    UIFont *font = [UIFont systemFontOfSize:13];
    
    // 該行要顯示的內容
    NSString *content = [data objectAtIndex:indexPath.row];
    // 計算出顯示完內容需要的最小尺寸
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
    
    // 構建顯示行
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
    // 設置顯示榘形大小
    rect.size = size;
    // 重置列文本區域
    cell.textLabel.frame = rect;
    
    cell.textLabel.text = content;
    
    // 設置自動換行(重要)
    cell.textLabel.numberOfLines = 0;
    // 設置顯示字體(一定要和之前計算時使用字體一至)
    cell.textLabel.font = font;

    return cell;
}

//===========//

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size
[label setNumberOfLines:0];
NSString *s = @"abcdefghijklmn";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];

////=============gengxing==========///

主要有两个地方需要调整高度,一个是自己创建的UILabel或其它,另一个就是cell的高度。在创建cell的地方只需要定义好label的属性就行了:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *cellIdentifier = @"cell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];  
  5.     if (cell == nil)  
  6.     {  
  7.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];  
  8.           
  9.         UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];  
  10.         [contentLabel setLineBreakMode:UILineBreakModeWordWrap];  
  11.         [contentLabel setNumberOfLines:0];  
  12.         [contentLabel setFont:FONT_CELL];  
  13.         [contentLabel setTag:TAG_LABEL];  
  14.           
  15.         [[cell contentView] addSubview:contentLabel];  
  16.         [contentLabel release];  
  17.     }  

设置完值后再设置frame:

  1. cell.textLabel.text = @"";  
  2. CGSize captionSize = [cell.textLabel.text sizeWithFont:FONT_CELL];  
  3. contentLabel.frame = CGRectMake(captionSize.width + 10, 0,   
  4.                                 CGRectGetWidth(cell.bounds) - captionSize.width - 10,   
  5.                                 [self tableView:nil heightForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]);  

我需要让cell的高度去适应label,所以调用计算cell高度的方法,在heightForRowAtIndexPath方法中计算label所需的高度即可:

  1. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.     if (indexPath.row < …) {  
  3.         return 90;  
  4.     } else if (indexPath.row == ...) {  
  5.         NSString *caption = @"";  
  6.         CGSize contentSize = getTextSize(caption, FONT_CELL, @"data",  
  7.                                          CGRectGetWidth(self.tableView.bounds));  
  8.         return contentSize.height > 44 ? contentSize.height : 44;  
  9.     }  
  10.     return 44;  
  11. }  

为了不让高度过于混乱,我把最小值设为44,getTextSize只是一个工具方法,用于计算x轴偏移的距离:

  1. CGSize getTextSize(NSString *offsetText,UIFont *font,NSString *text, CGFloat maxWidth){  
  2.     CGSize offsetTextSize = [offsetText sizeWithFont:font];  
  3.     CGSize textSize = [text sizeWithFont:font   
  4.      constrainedToSize:CGSizeMake(maxWidth-offsetTextSize.width, MAXFLOAT)   
  5.          lineBreakMode:UILineBreakModeWordWrap];  
  6.     return textSize;  
  7. }  

期间使用了一些公共宏,比如:FONT_CELL等等,只在cellForRowAtIndexPath里面设置好label的基本属性和frame,计算高度就交给heightForRowAtIndexPath,可以自己对返回值进行修改。

原文地址:https://www.cnblogs.com/jinjiantong/p/2975077.html