iphone:如何为滚动视图中的每个图像编码不同的标签

发布于 2024-12-08 13:18:14 字数 2106 浏览 0 评论 0原文

我成功地为 50 个图像创建了一个 UIscrollview(水平),并且还创建了一个“50 中的 1”页码的标签,该标签与滚动视图一起显示在图像顶部(例如,用户滚动到下一个图像,它将显示 50 中的 2) ,50 中的 3,依此类推)。我可以在图像底部显示标签作为描述。我的问题是如何创建或编码以显示每个图像的不同描述?我正在考虑 NSarray 但它显示了一些随机数。有没有一个示例我可以查看一下我做错了什么。你可以看到我从那里迷失的描述部分。谢谢=)

    CGFloat xOffset = 0;
        NSInteger pageNumber = 1;
            NSUInteger i;
        for (i = 1; i <= 5; i++)
        {
            NSString *imageName = [NSString stringWithFormat:@"creative%d.jpg", i];
            UIImage *image = [UIImage imageNamed:imageName];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

            [imageView setFrame:CGRectMake(xOffset, 0, 320, 288.5)];
            [ourScrollView addSubview:imageView];
            [imageView release];

            UILabel *pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, -10, 320, 40)];
            [pageNumberLabel setText:[NSString stringWithFormat:@"%d of 5", pageNumber]];
            [pageNumberLabel setTextAlignment:UITextAlignmentLeft];
            [pageNumberLabel setTextColor:[UIColor whiteColor]];
                    [pageNumberLabel setBackgroundColor:[UIColor clearColor]];
                    [pageNumberLabel setFont:[UIFont fontWithName:@"MarkerFelt-Thin" size:18]];
            [ourScrollView addSubview:pageNumberLabel];
            [pageNumberLabel release];

  NSArray  *description = [NSArray arrayWithObjects:@"wow", @"bam", @"pow", @"zing", @"bling",nil];                 

                    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, 250, 320, 40)];
                    [descriptionLabel setText:[NSString stringWithFormat:@"%@", description]];
                            [descriptionLabel setTextAlignment:UITextAlignmentLeft];
            [descriptionLabel setTextColor:[UIColor whiteColor]];
                    [descriptionLabel setBackgroundColor:[UIColor clearColor]];
            [ourScrollView addSubview:descriptionLabel];
            [descriptionLabel release];

            xOffset = xOffset + 320;
            pageNumber = pageNumber + 1;

I successfully created a UIscrollview (horizontal) for 50 images and also created a label of "1 of 50" page number displaying at the top of the image along with the scroll view(for example user scroll to next image it would show 2 of 50, 3 of 50, so forth). I am able to display a label at the bottom of the image as a description. My problem is how can I create or code to display different description for each image? Im thinking of NSarray but it shows some random numbers. Is there a sample I can look at to see what I am doing wrong. you can see the description part where I am lost from there. Thanks =)

    CGFloat xOffset = 0;
        NSInteger pageNumber = 1;
            NSUInteger i;
        for (i = 1; i <= 5; i++)
        {
            NSString *imageName = [NSString stringWithFormat:@"creative%d.jpg", i];
            UIImage *image = [UIImage imageNamed:imageName];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

            [imageView setFrame:CGRectMake(xOffset, 0, 320, 288.5)];
            [ourScrollView addSubview:imageView];
            [imageView release];

            UILabel *pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, -10, 320, 40)];
            [pageNumberLabel setText:[NSString stringWithFormat:@"%d of 5", pageNumber]];
            [pageNumberLabel setTextAlignment:UITextAlignmentLeft];
            [pageNumberLabel setTextColor:[UIColor whiteColor]];
                    [pageNumberLabel setBackgroundColor:[UIColor clearColor]];
                    [pageNumberLabel setFont:[UIFont fontWithName:@"MarkerFelt-Thin" size:18]];
            [ourScrollView addSubview:pageNumberLabel];
            [pageNumberLabel release];

  NSArray  *description = [NSArray arrayWithObjects:@"wow", @"bam", @"pow", @"zing", @"bling",nil];                 

                    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, 250, 320, 40)];
                    [descriptionLabel setText:[NSString stringWithFormat:@"%@", description]];
                            [descriptionLabel setTextAlignment:UITextAlignmentLeft];
            [descriptionLabel setTextColor:[UIColor whiteColor]];
                    [descriptionLabel setBackgroundColor:[UIColor clearColor]];
            [ourScrollView addSubview:descriptionLabel];
            [descriptionLabel release];

            xOffset = xOffset + 320;
            pageNumber = pageNumber + 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2024-12-15 13:18:14

您正在使用@"%d" 格式设置descriptionLabel,这是一种十进制格式。什么是“描述”?如果它是 NSString 那么你应该使用 @"%@"

You're setting the descriptionLabel with a format of @"%d", which is a decimal format. What is "description"? If it's a NSString then you should be using @"%@".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文