2012-06-03 2 views
0

두 개의 레이블이 있고 테이블보기 셀에 둘 다 표시하려고합니다. 하지만 하나씩 표시되는 대신 두 번째 레이블이 첫 번째 레이블에 표시되고 첫 번째 레이블은 표시되지 않습니다. 첫 번째 레이블 다음에 두 번째 레이블을 정확히 표시하려면 어떻게해야합니까?iOS에서 테이블보기 셀에 두 개의 레이블을 차례로 넣는 방법은 무엇입니까?

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

답변

1

두 프레임 모두 다른 프레임을 사용하는 동일 프레임 사용

0

당신은 결코 두 UILabels에 대한 프레임을 설정하지 않습니다 다음은 내 코드입니다. label과 secondLabel 모두 initWithFrame : CGRectZero를 호출하므로 두 경우 모두 해당 프레임의 origin.x와 origin.y는 0입니다.

secondLabel은 기본적으로 불투명으로 표시되어 첫 번째 레이블을 완전히 모호하게 만듭니다 .

이 문제를 해결하려면 레이블에 UITableViewCell에 추가 된 후 실제로 setText :를 호출해야합니다. 레이블 크기가되면 secondLabel의 프레임을 그 아래에 설정할 수 있습니다.

관련 문제