Search code examples
iosuitableviewlabelcell

How to put two labels in a table view cell one after another on iOS?


I have two labels and trying to display both of them on a table view cell. But instead of being displayed one after another, the second label displays itself on the first one and the 1st one is never displayed... How can I display the second label exactly after the first label? Here is my code :

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell;
UILabel *label=nil , *secondLabel = nil;

cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"] autorelease];

    label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setMinimumFontSize:FONT_SIZE];
    [label setNumberOfLines:0];
    [label setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
    [label setTag:1];

    //[[label layer] setBorderWidth:2.0f];

    [[cell contentView] addSubview:label];

    secondLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [secondLabel setLineBreakMode:UILineBreakModeWordWrap];
    [secondLabel setMinimumFontSize:FONT_SIZE];
    [secondLabel setNumberOfLines:0];
    [secondLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    [secondLabel setTag:2]; 

    [[cell contentView] addSubview:secondLabel];    
}


//NSString *text = [items objectAtIndex:[indexPath row]];
int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
NSString *text1 = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];  
NSString *text2 = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];    

if (!label)
    label = (UILabel*)[cell viewWithTag:1];
if (!secondLabel)
    secondLabel = (UILabel*)[cell viewWithTag:2];    

//[label setText:text1];
//[secondLabel setText:text2];

label.text = text1;
secondLabel.text = text2;

//cell.textLabel.text = text1;
//cell.detailTextLabel.text = text2;

[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size1.height, 44.0f))];
[secondLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size2.height, 44.0f))]; 

return cell;  }

and:

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

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *text1 = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *text2 = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];    

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];    
CGFloat height = MAX(size1.height + size2.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2);}

Solution

  • The problem is that you are using the same frame for both labels. Use different frames.