2015-10-06 43 views
2

我重視的Objective-C試圖創建在NSArray中每個項目的標籤 - 不工作

enter image description here

,看起來就像圖像的陣列和我所試圖做的是以下幾點:

  • 運行爲一個foreach此的NSArray
  • 陣列中創建的每個項目的標籤(SO 3個司標籤,3個項目標籤,3個工作單標籤和3組裝標籤)

我有以下代碼:

int counter = 0; 
    for (id object in _theParamenterArray) { 
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)]; 
     label.text = [object objectForKey:@"Assemble"]; 
     [label setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:label]; 

     UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)]; 
     label.text = [object objectForKey:@"Division"]; 
     [label setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelTwo]; 

     UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)]; 
     label.text = [object objectForKey:@"Project"]; 
     [label setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelThree]; 

     UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)]; 
     label.text = [object objectForKey:@"WorkOrder"]; 
     [label setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelFour]; 
     counter++; 
    } 

我用這個代碼的問題是,它只有在共造成4個標籤和數據是數組中的最後一個項目,所以纔出現了壓倒一切的價值觀。我的問題我該如何解決這個問題?請幫忙!

+1

你確定你的循環執行了三次嗎?我注意到調試器中顯示的數組和代碼中的數組有不同的名稱 – Paulw11

回答

1

看起來像你的問題是分配文本和字體label 4倍,試試這個:

int counter = 0; 
    for (id object in _theParamenterArray) { 
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)]; 
     label.text = [object objectForKey:@"Assemble"]; 
     [label setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:label]; 

     UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)]; 
     labelTwo.text = [object objectForKey:@"Division"]; 
     [labelTwo setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelTwo]; 

     UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)]; 
     labelThree.text = [object objectForKey:@"Project"]; 
     [labelThree setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelThree]; 

     UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)]; 
     labelFour.text = [object objectForKey:@"WorkOrder"]; 
     [labelFour setFont:[UIFont boldSystemFontOfSize:16]]; 
     [self.view addSubview:labelFour]; 
     counter++; 
    } 

這將有助於看到數組的代碼。

4

你只爲 「標籤」 變量:)這是一個複製/粘貼錯誤:)
重命名標籤設置文本 - > labelTwo,labelThree,labelFour爲線:

label.text = [object objectForKey:@"WorkOrder"]; 
[label setFont:[UIFont boldSystemFontOfSize:16]]; 
相關問題