的Objective-C试图创建在NSArray中每个项目的标签 - 不工作

问题描述:

我重视的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

看起来像你的问题是分配文本和字体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++; 
    } 

这将有助于看到数组的代码。

你只为 “标签” 变量:)这是一个复制/粘贴错误:)
重命名标签设置文本 - > labelTwo,labelThree,labelFour为线:

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