2011-07-27 4 views
0

다음 코드에서는 프로그래밍 방식으로 스크롤 가능보기의 각 행에 두 개의 레이블을 추가하려고합니다. 뷰에서 "label"이 문제없이 추가되지만 "infoLabel"은 마지막 행만 표시합니다. 알아 내려고 애 쓰고 있습니다. 도움 주셔서 감사합니다.마지막 레이블 만 표시됩니다.

- (void)refreshList 
{ 
    // remove all the labels from the GUI 
    for (UILabel *label in scrollView.subviews) 
     [label removeFromSuperview]; 
    RootModel *rm = [RootModel sharedModel]; 

    float labelOffset = LABEL_SPACING; // reset the spacing 
    UILabel *infoLabel = [[UILabel alloc] init ]; 

    // repopulate the scroll view with Labels 
    for (UILabel *label in labels) 
    { 
     NSLog(@"%@", label.text); 
     CGRect labelFrame = label.frame; // fetch the frame of label 

     labelFrame.origin.x = LEFT_LABEL_SPACING; // set the x-coordinate 
     labelFrame.origin.y = labelOffset; // set the y-coordinate 
     labelFrame.size.width = scrollView.frame.size.width/3; 
     labelFrame.size.height = LABEL_HEIGHT; // set the height of Label 
     label.frame = labelFrame; // assign the new frame to Label 
     [scrollView addSubview:label]; // add Label as a subview 

     CGRect infolabelFrame = infoLabel.frame; // fetch the frame of label 

     infolabelFrame.origin.x = 10; // set the x-coordinate 
     infolabelFrame.origin.y = labelOffset; // set the y-coordinate 
     infolabelFrame.size.width = scrollView.frame.size.width/3; 
     infolabelFrame.size.height = LABEL_HEIGHT; // set the height of Label 
     infoLabel.frame = infolabelFrame; // assign the new frame to Label 

     NSString *key = label.text; 
     NSLog(@"%@", key); 
     infoLabel.text = [rm.rLevels valueForKey:key]; 
     NSLog(@"%@ %@", infoLabel.text, key); 

     infoLabel.frame = infolabelFrame; // fetch the frame of label 
     [infoLabels addObject:infoLabel]; 
     [scrollView addSubview:infoLabel]; // add Label as a subview 

     // increase the offset so the next button is added further down 
     labelOffset += LABEL_HEIGHT + LABEL_SPACING; 
    } // end for 
} // end refreshList 

답변

2

당신은 infoLabel의 단일 인스턴스를 생성하고 루프의 각 반복에 그 frame를 재설정하고 있습니다. 레이블이 여러 개인 경우 루프의 각 반복마다 레이블을 할당하고 초기화해야합니다. 하위 뷰로 추가 한 후 할당하려는 레이블의 소유권을 잃어 버리지 않았는지 확인하십시오.

+0

당신이 말하는 것은 완벽한 말이됩니다. 그래서 나는이 문장을 다음과 같이 변경했다 : [scrollView addSubview : [infoLabels objectAtIndex : [infoLabels count] -1]]; // 라벨을 하위보기로 추가합니다. 라벨과 마찬가지로 infoLabels이 인터페이스에 정의되어 있습니다. 여전히 같은 문제입니다. – saman01

+0

미안해. 나 해보자. – saman01

관련 문제