Dynamic Height of UITableViewCell with custom content


#define FONT_SIZE 12.0f

//#define tableView.frame.size.width  320.0f

#define CELL_CONTENT_MARGIN 2.0f

// 65 = image height if you have image in custom UITableViewCell

-(double)heightCalculate:(NSString *)calculateText{

    UILabel *calculateText_lbl = [[UILabel alloc] init];

    [calculateText_lbl setLineBreakMode:UILineBreakModeClip];

    // [descrtiption_lbl setBackgroundColor:[UIColor grayColor]];

    [calculateText_lbl setMinimumFontSize:FONT_SIZE];

    [calculateText_lbl setNumberOfLines:0];

    [calculateText_lbl setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];

    [calculateText_lbl setFont:[UIFont systemFontOfSize:FONT_SIZE]];

    NSString *text = calculateText;

    CGSize constraint = CGSizeMake(self.view.frame.size.width  - (CELL_CONTENT_MARGIN * 2), FLT_MAX);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    [calculateText_lbl setText:text];

    [calculateText_lbl setFrame:CGRectMake(65, CELL_CONTENT_MARGIN,  ((self.view.frame.size.width  - (CELL_CONTENT_MARGIN * 2)) -65),size.height)];

    [calculateText_lbl sizeToFit];

    double height_lbl = calculateText_lbl.frame.size.height;

    [calculateText_lbl release];

    return (height_lbl);

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *userActivity  = [NSString stringWithFormat:@"%@",[self.arrray1 objectAtIndex:indexPath.row ]];

 NSString *text1 = [NSString stringWithFormat:@"Posted by %@ on %@",[self.array2 objectAtIndex:indexPath.row ];

  double _height = [self heightCalculate:userActivity];

    double _height1 = [self heightCalculate:text1];

    double height;

        height= MAX((_height+_height1+15), 65.0f); 

    }

    return (height + (CELL_CONTENT_MARGIN * 3));

    

}

Next Recommended Reading Custom Overlay - Simple Example