为什么UILabel文本仅在UIScrollView的第一页上

时间:2010-09-18 15:49:24

标签: iphone uiscrollview uilabel

我的scrollView显示从服务器下载的图像。现在我想在UILabel中显示其他信息。但是,这仅在第一张图像下显示。滚动UILabel时不显示: - (

(void)populateImageArray:(NSNotification *)notif 
{
    NSUInteger i;
    CGFloat contentOffset = 0.0f;

    for (i = 0; i <= [items count]-1; i++)
    {    
        CGRect imageViewFrame = CGRectMake(contentOffset, 0.0f, scrollViewPreview.frame.size.width, scrollViewPreview.frame.size.height);
//        CGRect rect = CGRectMake(139 , 405, 120.0f, 45.0f);
        CGRect rect = CGRectMake(0 , 330, 120.0f, 45.0f);

        NSString *titleString, *preisString, *resultString;
        titleString = [[items objectAtIndex:i] title];
        preisString = [[items objectAtIndex:i] price];
        resultString = @"";
        resultString = [resultString stringByAppendingString:titleString];
        resultString = [resultString stringByAppendingString:@"\n"];
        resultString = [resultString stringByAppendingString:preisString];

        UILabel *text = [[UILabel alloc] initWithFrame:rect];
        [text setTextColor:[UIColor redColor]];
        [text setNumberOfLines:0];
        text.textAlignment=UITextAlignmentCenter;

        [scrollViewPreview addSubview:text];

        text.text = [NSString stringWithFormat:@"%d",resultString];

        NSURL *urlString = [NSURL URLWithString:[[items objectAtIndex:i] bilder]];
        NSData *data = [NSData dataWithContentsOfURL:urlString];
        UIImage *image = [[UIImage alloc] initWithData:data];
        //UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
        [imageView setImage:image];
        //imageView.contentMode = UIViewContentModeCenter;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [scrollViewPreview addSubview:imageView];
        [text release]; 
        [imageView release];
        [titleString release];
        [preisString release];
        //[resultString release];

        contentOffset += imageView.frame.size.width;
        scrollViewPreview.contentSize = CGSizeMake(contentOffset,     scrollViewPreview.frame.size.height);

    }
    scrollViewPreview.clipsToBounds = NO;
    scrollViewPreview.pagingEnabled = YES;
    scrollViewPreview.showsHorizontalScrollIndicator = YES;

1 个答案:

答案 0 :(得分:1)

更改下一行:

CGRect rect = CGRectMake(0 , 330, 120.0f, 45.0f);

为:

CGRect rect = CGRectMake(contentOffset , 330, 120.0f, 45.0f);

问题是您创建的所有标签都位于同一个地方......