【问题标题】:Why is the UILabel text only on the first page of the UIScrollView为什么UILabel文本只在UIScrollView的第一页
【发布时间】:2010-09-18 15:49:24
【问题描述】:

我的 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;

【问题讨论】:

  • 我已经格式化了您的问题 - 每个源代码行必须以至少 4 个空格开头,以便像代码一样显示...

标签: iphone uiscrollview uilabel


【解决方案1】:

更改下一行:

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

到:

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

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

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多