2011-07-27 143 views
0

在下面的代碼中,我打算以可編程方式在視圖的每一行中以編程方式添加兩個標籤。 「標籤」被添加到視圖中,但沒有問題,但「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]]; //將Label添加爲子視圖。 infoLabels在界面中定義,就像標籤一樣。仍然是同樣的問題。 – saman01

+0

對不起,我明白了。讓我嘗試。 – saman01

相關問題