Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSArray中每个项目的Objective-C创建UI标签_Ios_Objective C - Fatal编程技术网

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

Ios NSArray中每个项目的Objective-C创建UI标签,ios,objective-c,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:lab

我这里有一段代码:

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行,每行为以下值:
assembly
Division
Project
WorkOrder
,我想将每个值放在
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,我试图从每一行获取所有值

以下是我的阵列的屏幕截图:


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

你确实会得到四个看起来像一个的标签,因为它们彼此重叠,并且都说相同的话(即“自定义标签”)

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

  • 确保为标签提供不同的框架-制作一个计数器,确定标签之间的距离,并相应地调整坐标,以及
  • 将不同的文本设置到标签中-例如,使用
    label.text=object
    (假设
    object
    NSString
调整框架原点的方式取决于标签彼此之间的位置。要垂直堆叠,请使用以下代码:

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*计数器表达式将标签放置在

(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)

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


我看不出你提供的代码有任何问题,你能进一步详细说明你的问题吗?我的问题是,当我运行此代码时,我只得到一个标签,上面写着自定义标签。如果你在同一帧设置所有标签,请更改每个标签的帧。对象是什么类型的?您可以将其称为
id
,但如果要从中获取值,使用已知的对象类型会更容易。您为什么不选择
UITableView
?对我来说,这听起来像是一个完美的表格视图用例。你正在创建的标签高度不断增长,但开始于相同的坐标。它们将重叠。请尝试使用CGRectMake(10,x,200,35)
代替。:-)(10,10)或(10,20)?@T先生,谢谢!这是OP代码的复制粘贴错误,这是我的代码的早期版本的复制粘贴:)@dasblinkenlight仍有问题……显示的只是每个代码的工作顺序item@user979331你看到编辑后我对代码所做的更改了吗?我的意思是,10、110、210和310部分?@dasblinkenlight是的,我附上了我的阵列的截图
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];
}