views:

54

answers:

2

My scrollView shows images downloaded from a server. Now i want to show additional information in an UILabel. However this is only shown under the first image. When scrolling the UILabel is not shown :-(

(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  A: 

Change the next line:

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

to:

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

The problem is that all the labels that you create are located in the same place...

Michael Kessler
A: 

Thanks a lot! It works!

Thomas
Happy that I could help you. I will appreciate if you will accept my answer. Also you should have posted this as a comment to my answer and not as a separate answer...
Michael Kessler