0

我想根据数组 count 以编程方式生成一些标签。

我为此找到了很多链接,但无法得到正确的答案。我正在尝试生成

这些标签在 for 循环中。任何人都可以给我一些编码示例来满足我的要求吗?

4

2 回答 2

1

像这样的东西?

float y = 40;
for (int i = 0; i < [myArray count]; i++) {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, y, 300, 30)];
    [label setText:[myArray objectAtIndex:i]];
    [self.view addSubview:label];
    y += 40;
}

这给出了如何在循环中生成标签的简单演示,它们的 y 原点每次递增 40。

于 2012-10-19T06:17:14.837 回答
0
NSMutableArray *arr=[[NSMutableArray alloc]init];
//generate labels like this
for(int i=0;i<5;i++){
//set the frame or add to view or do anything with your label
UILabel *lbl=[[UILabel alloc] init];

[arr addObject:lbl];
}

//When you need to use , just iterate through the array and cast the objects back into UILabel
Uilabel *temp;
for(temp in arr){
UILabel *lbl=(UILabel*)temp;  
}
于 2012-10-19T06:16:33.937 回答