OGeek|极客世界-中国程序员成长平台

标题: ios - 在 ViewDidLoad 中为 UICollectionViewController 解析查询 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:03
标题: ios - 在 ViewDidLoad 中为 UICollectionViewController 解析查询

我正在尝试调整 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 *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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 with array of local images

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

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

PFQuery *query = [PFQuery queryWithClassName"hoto"];

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 arrayWithArraybjects];
        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 想要使用数组中的信息来填充单元格时,那里什么都没有。

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

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

任何反馈都将不胜感激。



Best Answer-推荐答案


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

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

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

- (void)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName"hoto"];
    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] initWithCapacitybjects.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 *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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 with Parse images

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

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

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

干杯,

关于ios - 在 ViewDidLoad 中为 UICollectionViewController 解析查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475224/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4