试图将UILabel添加到由2列组织的某些UIView中

时间:2011-07-16 16:08:56

标签: iphone uiview uilabel

我将n UILable添加到由2列组织的n UIView中,但只显示了第一个UILabel。

这是我的代码:

for(int i=0; i<ieneN; i++){
    UIView *ienaSx = [[UIView alloc] init];
    UIView *ienaDx = [[UIView alloc] init];

    ienaSx.frame = CGRectMake(0.0, i*95.0, 160.0, 95.0);
    if(i%2==0)
        ienaSx.backgroundColor = [UIColor redColor];
    else 
        ienaSx.backgroundColor = [UIColor greenColor];

    UILabel *ienaSxLabel = [[UILabel alloc] init];
    ienaSxLabel.frame = CGRectMake(0.0, (i*95.0)+80.0, 160.0, 15.0);
    ienaSxLabel.text = [NSString stringWithFormat:@"Iena n°: %i", i];
    [ienaSx addSubview:ienaSxLabel];

    [scrollView addSubview:ienaSx];

    ienaDx.frame = CGRectMake(160.0, i*95.0, 160.0, 95.0);

    if(i%2==0)
        ienaDx.backgroundColor = [UIColor greenColor];
    else 
        ienaDx.backgroundColor = [UIColor redColor];        

    UILabel *ienaDxLabel = [[UILabel alloc] init];
    ienaDxLabel.frame = CGRectMake(160.0, (i*95.0)+80.0, 160.0, 15.0);
    ienaDxLabel.text = [NSString stringWithFormat:@"Iena n°: %i", i+1];

    [ienaDx addSubview:ienaDxLabel];


    [scrollView addSubview:ienaDx];
}

请参阅此图片以获得更清晰的解释:http://imageshack.us/photo/my-images/269/schermata20110716a18064.png/

有人可以告诉我我哪里错了吗?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

尝试使用此设置标签的框架:

ienaSxLabel.frame = CGRectMake(0.0, 80.0, 160.0, 15.0);

相同
ienaDxLabel.frame = CGRectMake(0.0, 80.0, 160.0, 15.0);

另外,在将对象添加到超级视图后,不要忘记释放它们(视图和标签)。