如何在iphone的数组索引中显示标签中的值



我有一个数组,有两个项目,我想如果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];
}

标签重叠。试着

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];

}

变化:

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

:

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

添加:

[scrollView addSubview:DescriptionLabel];
[DescriptionLabel release];

你的代码将一个接一个地重叠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
}

最新更新