2

我正在尝试调整 UICollectionViewController 的使用,我正在填充一个本地图像数组以从 Parse 获取图像。

到目前为止,它非常简单。我的 NSArray 多次填充相同的本地图像:

testImages = [NSArray arrayWithObjects: @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @" thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail.jpg", @"thumbnail .jpg", @"thumbnail.jpg", @"thumbnail.jpg", nil];

在 collectionView:cellForItemAtIndexPath: 我确实设置了我的单元格(来自 Storyboard):

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Set up cell identifier that matches the Storyboard cell name
    static NSString *identifier = @"Cell";    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    // Configure the cell to show photo thumbnail
    UIImageView *testImageView = (UIImageView *)[cell viewWithTag:100];

    testImageView.image = [UIImage imageNamed:[testImages objectAtIndex:indexPath.row]];

    return cell;
}

这是有效的,看起来像这样:

带有本地图像数组的 UICollectionViewController

我想要做的是用我从 Parse 的照片类中得到的图片替换本地创建的数组。

我正在尝试在 viewDidLoad 方法上执行此操作:

PFQuery *query = [PFQuery queryWithClassName:@"Photo"];

PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];
[query orderByAscending:@"createdAt"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];


[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %d photos.", objects.count);
        testImages = [NSMutableArray arrayWithArray:objects];
        NSLog(@"# Images: %d", [testImages count]);

        // Do something with the found objects
        for (PFObject *object in objects) {                
            NSLog(@"Object Name: %@", object.objectId);
        }


    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

问题是我每次都得到一个空数组。我猜想,因为一旦 collectionView:numberOfItemsInSection: 询问“testImages”数组上的元素计数,我总是得到 0 个元素,因为这是在后台的块上执行的。

当 UICollectionViewController 想要使用数组中的信息来填充单元格时,那里什么都没有。

我不知道我是否将代码放在错误的位置,或者我是否使用了错误的查询。

你能在这里得到我的错误吗?

任何反馈将不胜感激。

4

1 回答 1

4

最后我得到了一些工作。@gg13 的反馈是解决空数组问题的关键。

我将把解决方案留在这里,以防它在其他时候对其他人有所帮助。

我把我的查询放在一个新方法上:

- (void)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
    PFUser *user = [PFUser currentUser];
    [query whereKey:@"user" equalTo:user];
    [query orderByAscending:@"createdAt"];
    [query setCachePolicy:kPFCachePolicyNetworkOnly];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d photos.", objects.count);

            [self.collectionView reloadData];

            gridImages = [[NSMutableArray alloc] initWithCapacity:objects.count];

            // Do something with the found objects
            for (PFObject *object in objects) {

                PFFile *thumbnail = [object objectForKey:@"thumbnail"];
                [thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    if (!error) {
                        // Now that the data is fetched, update the cell's image property with thumbnail

                        NSLog(@"Fetching image..");

                        [gridImages addObject:[UIImage imageWithData:data]];

                        NSLog(@"Size of the gridImages array: %d", [gridImages count]);

                    } else {
                        // Log details of the failure
                        NSLog(@"Error: %@ %@", error, [error userInfo]);
                    }
                }];
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

我正在调用 viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self queryForTable];
}

然后我将我的 collectionView:cellForItemAtIndexPath: 方法更改为:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Set up cell identifier that matches the Storyboard cell name
    static NSString *identifier = @"Cell";    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    // Configure the cell to show photo thumbnail
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];

    //NSLog(@"testImages: %@", [testImages objectAtIndex:indexPath.row]);
    imageView.image = [UIImage imageNamed:@"placeholder.jpg"];

    imageView.image = [gridImages objectAtIndex:indexPath.row];

    return cell;
}

到目前为止,它正在工作。该应用程序正在从 Parse 获取图像,它看起来像这样:

带有解析图像的 UICollectionViewController

我仍然需要测试一些东西,特别是具有大量图像(包括进度指示器或其他东西)的场景,但至少要做到这一点已经是一件好事。

我确信 Parse 迟早会像他们对 UITableViewController 所做的那样制作自己的 UICollectionViewController,并且事情会进一步改善。

如前所述,感谢您的反馈,我将其留在这里,以防其他人遇到同样的问题,当然也愿意接受反馈和建议。

干杯,

于 2013-07-06T14:21:42.567 回答