0

我有两个项目的数组我想要如果 i=0 那么它可能显示 Ali 和 1 然后 Jawaad 但我不希望它是静态的我想要动态的如果数组中有 100 个项目所以它们必须根据它们的索引给出标签我正在使用循环,但这个循环总是将 Jawaad 设置为标签。

NSArray*myArray = [[NSArray alloc] initWithObjects:@"Ali",@"Jawaad",nil];
int countTest=[myArray count];
NSLog(@"count Test is %d",countTest);

for (int i=0; i<countTest; i++) {

    DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,20,206,84)];
    DescriptionLabel.textAlignment =  UITextAlignmentLeft;
    DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    DescriptionLabel.numberOfLines = 0;

    DescriptionLabel.textColor = [UIColor blackColor];

    NSString*testting=[myArray objectAtIndex:i];
    DescriptionLabel.text=testting;
    DescriptionLabel.font = [UIFont fontWithName:@"Helvetica" size:16];

    [scrollView addSubview:DescriptionLabel];
}
4

3 回答 3

0

改变 :

DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,20,206,84)];

到 :

DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(i*206,20,206,84)];

添加 :

[scrollView addSubview:DescriptionLabel];
[DescriptionLabel release];
于 2013-05-16T10:10:17.570 回答
0

标签重叠。尝试

int y = 20;
for (int i=0; i<countTest; i++) {

    DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,y,206,84)];
y = y+ 84;
    DescriptionLabel.textAlignment =  UITextAlignmentLeft;
    DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    DescriptionLabel.numberOfLines = 0;

    DescriptionLabel.textColor = [UIColor blackColor];


    NSString*testting=[myArray objectAtIndex:i];
    DescriptionLabel.text=testting;
    DescriptionLabel.font = [UIFont fontWithName:@"Helvetica" size:16];

    [scrollView addSubview:DescriptionLabel];



}
于 2013-05-16T10:11:12.023 回答
0

您的代码将不断重叠UILabel,因为它们frames是相同的。您应该动态增加框架的 y 。

int y = 10;
for (int i=0; i<countTest; i++) {

    DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,y,206,84)];
    DescriptionLabel.textAlignment =  UITextAlignmentLeft;
    DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    DescriptionLabel.numberOfLines = 0;

    DescriptionLabel.textColor = [UIColor blackColor];


    NSString*testting=[myArray objectAtIndex:i];
    DescriptionLabel.text=testting;
    DescriptionLabel.font = [UIFont fontWithName:@"Helvetica" size:16];

    [scrollView addSubview:DescriptionLabel];

    y = y+30
}
于 2013-05-16T10:13:49.097 回答