NSArray中每个项目的Objective-C创建UI标签

时间:2015-10-05 14:47:38

标签: ios objective-c

我在这里有这段代码:

for (id object in _theParamenterArray) {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
    label.text = @"Custom Label";
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    [self.view addSubview:label];
}

我要做的是为NSArray中的每个项目创建一个标签。我的数组有3行,每行有以下值:AssembleDivisionProjectWorkOrder,我想将每个值放在UILabel中。我将如何做到这一点?

我尝试了以下内容:

    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++;
}

但它只显示所有三行的WorkOrder,我试图从每一行获取所有值。

以下是我的阵列的截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

  

我的问题是,当我运行此代码时,我只得到一个标签,上面写着"自定义标签"

你会得到四个看起来像一个的标签,因为它们彼此叠加,并且都说同样的东西(即"自定义标签")。

为了解决这个问题,你需要做两件事:

  • 确保为您的标签提供不同的框架 - 制作一个计数器,决定标签应该相隔多远,并相应地调整坐标,
  • 将不同的文字设置为标签 - 例如,使用label.text = object(假设objectNSString

调整框架原点的方式取决于您希望标签相对于彼此的位置。要垂直堆叠它们,请使用以下代码:

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++;
}

10+30*counter表达式将您的标签置于

位置
(10, 10)   (110, 10)  (210, 10)  (310, 10)
(10, 40)   (110, 40)  (210, 40)  (310, 40)
(10, 70)   (110, 70)  (210, 70)  (310, 70)
(10, 100) (110, 100) (210, 100) (310, 100)

答案 1 :(得分:0)

您需要像下面一样增加Y值(或者将CGRect设置为您想要标签的位置)

NSArray *obj = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
int x = 200;
for (NSString *object in obj) {

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, x, 200, 20)];
    x+= 40;
    label.text = object;
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    [self.view addSubview:label];
}