I’m trying to show custom UILabel in UITableViewCell, but something wrong.
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *currentComment = [comments objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"TitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
int commentLevel = [[currentComment objectForKey:@"level"] intValue];
NSLog(@"Comment level: %i", commentLevel);
NSString *commentText = [currentComment objectForKey:@"text"];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
[titleLabel setFont:[UIFont fontWithName:@"Verdana" size:17.0]];
CGSize textSize;
if (commentLevel == 0) {
textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(310, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
//titleLabel.bounds = CGRectMake(5, 5, textSize.width, textSize.height);
} else {
textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake((295-10*commentLevel), FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
//titleLabel.bounds = CGRectMake(15, 5, textSize.width, textSize.height);
}
titleLabel.bounds = CGRectMake(20, 20, 300, 100);
titleLabel.text = commentText;
[cell.contentView addSubview:titleLabel];
return cell;
}
Screenshot of result: 
Are you sure that you are getting the comment values because by looking into the screen shot it appears as if you are setting the garbage to the label’s text.
Also you must set the frame of the label instead of its bounds because you want it location to be relevant to the UITableViewCell.
change the code
titleLabel.bounds = CGRectMake(20, 20, 300, 100);
to
titleLabel.frame = CGRectMake(20, 20, 300, 100);