"Creating multiline cell in UITableView"

Tue 07 September 2010

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"BigListCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier] autorelease];

CGRect labelFrame =

CGRectMake(CGRectGetMinX(cell.frame)+50,

  CGRectGetMinY(cell.frame), CGRectGetWidth(cell.frame)-150,

  CGRectGetHeight(cell.frame));

UILabel *label = [[UILabel alloc]

  initWithFrame:labelFrame];

label.numberOfLines =0;

label.tag = 100;

label.lineBreakMode = UILineBreakModeWordWrap;

label.autoresizingMask = UIViewAutoresizingFlexibleWidth

|UIViewAutoresizingFlexibleHeight;

[cell.contentView addSubview:label];

[label release];

}

UILabel *label = (UILabel*)[cell viewWithTag:100];

label.text = @"Long Long Long Long Long Long Long Long Long Long Text"; // Put your text there

}

 

- (CGFloat)tableView:(UITableView *)tableView 

heightForRowAtIndexPath:(NSIndexPath *)indexPath{

CGSize constraintSize = CGSizeMake(170.f, CGFLOAT_MAX);

UIFont *theFont  = [UIFont boldSystemFontOfSize:15.0f];

CGSize theSize = [theText sizeWithFont:theFont

constrainedToSize:constraintSize

lineBreakMode:UILineBreakModeWordWrap];

return theSize.height+20;

}