如何在iOS上一个接一个地在表格视图单元格中放置两个标签?

时间:2012-06-03 21:30:09

标签: ios uitableview label cell

我有两个标签,并尝试在表格视图单元格上显示它们。但是,不是一个接一个地显示,第二个标签显示在第一个标签上,第一个标签从未显示... 如何在第一个标签后准确显示第二个标签? 这是我的代码:

- (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;  }

-(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);}

2 个答案:

答案 0 :(得分:1)

两个标签使用相同的框架使用不同的框架

答案 1 :(得分:0)

您永远不会为两个UILabel设置帧。对于label和secondLabel,你调用的是initWithFrame:CGRectZero,所以在这两种情况下,它们的frame的origin.x和origin.y都是0。

默认情况下,secondLabel被标记为不透明,这就是它完全遮盖第一个标签的原因。

要解决此问题,您应该在将标签添加到UITableViewCell后在标签上调用setText :.获得标签大小后,您可以将secondLabel的框架设置为低于该标签。