0

您将如何更改此方法以允许图像通过数组而不是颜色进入?因为我有 mySQL 数据库,它传入 url,这些 url 在 xcode 中分配为 NSStrings,存储在数组中。

想法是每个视图都设置了来自 mySQL 数据库的背景图像。

  - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor],
                               [UIColor greenColor],[UIColor yellowColor] , nil];

            for (int i = 0; i < colors.count; i++) {

                CGRect frame;
                frame.origin.x = self.scrollView.frame.size.width * i;
                frame.size = self.scrollView.frame.size;

                self.scrollView.pagingEnabled = YES;

                UIView *subview = [[UIView alloc] initWithFrame:frame];
                subview.backgroundColor = [colors objectAtIndex:i];

                [self.scrollView addSubview:subview];
            }

            self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

    }

我尝试了(introNames 是从 mySQL 数据库中保存图像 id 和名称的类。imageArray 是在上面的代码类中创建的,并且来自 introNames 的 nsstrings 被分配给它)。

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i < imageArray.count; i++) {

        introImages *snap = [imageArray objectAtIndex:i];

        imageName2 = snap.imageName;

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.size = self.scrollView.frame.size;

        self.scrollView.pagingEnabled = YES;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName2]];

        [subview addSubview:backgroundView];
        [self.scrollView addSubview:subview];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);

}

这是工作数据检索系统。

-(void) retrieveData
{
    NSURL *url = [NSURL URLWithString:getDataUrl];
    NSData *data = [NSData dataWithContentsOfURL:url];

    if (data) {

        jsonConnection = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        imageArray = [[NSMutableArray alloc]init];

        for (int i = 0; i < jsonConnection.count; i++)
        {
            NSString *pID = [[jsonConnection objectAtIndex:i] objectForKey:@"id"];
            NSString *pName = [[jsonConnection objectAtIndex:i] objectForKey:@"ImagesURL"];

            introImages *myImages = [[introImages alloc]initWithimageID:pID andimageName:pName];

            [imageArray addObject:myImages];
        }

    }

    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Something is wrong with your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }

任何想法都会很棒:)

4

1 回答 1

0

如果您没有在应用程序中使用 iOS6+,请使用此类PSTCollectionView ,如果您使用的是 iOS6 及更高版本,请使用UICollectionView 类参考

于 2013-07-15T06:02:31.943 回答