解析 UICollectionView 的查询不工作

Parse Query For UICollectionView not working

目前,我正在尝试通过 UICollectionView 中的解析查询来实现我的图像。最初我是这样做的:

这是我的 viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"View Did Load");
    // Do any additional setup after loading the view, typically from a nib.

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

    self.dataArray = [query findObjects];
    [self setupCollectionView];





}

这是我的 cellForItemAtIndexPath:

-(CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Row: %ld",(long)indexPath.row);
    CMFGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    NSString *imageName = @"Picture";
    if(imageName == nil){
        NSLog(@"imageName is nil");
    }
    [cell setImageName:imageName];
    PFFile *file = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"image"];
    NSLog(@"filename = %@",[file name]);
    cell.imageView.file = file;
    [cell.imageView loadInBackground];

    return cell;

}

但出于某种原因,即使我的一些调试代码输出了检索到的 19 个对象,但在应该有 19 个的时候只显示了一张图像(每个对象都有一个 PFFile 图像)

然后,我把后一种方法改成这样:

-(CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Row: %ld",(long)indexPath.row);
    CMFGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    PFQuery *newQuery =[PFQuery queryWithClassName:@"SwapPost"];
    NSArray *newArray = [newQuery findObjects];
    NSLog(@"%@",[[newArray objectAtIndex:indexPath.row] objectId]);
    NSString *imageName = @"Picture";
    if(imageName == nil){
        NSLog(@"imageName is nil");
    }
    [cell setImageName:imageName];
    PFFile *file = [[newArray objectAtIndex:indexPath.row] objectForKey:@"image"];
    NSLog(@"filename = %@",[file name]);
    cell.imageView.file = file;
    [cell.imageView loadInBackground];

    return cell;

}

这显示了我想要的所有 19 张图像..但是我正在实现一个水平图像滚动器,一次一张图像(所以一次一个单元格),这意味着每次用户滚动查询一遍又一遍地调用 findObjects 这会在网络上变得非常麻烦并使应用程序滞后。有没有办法修复它,让我只需要调用一次 findObjects 就可以顺利显示所有 19 张图像?

我认为您对 UICollectionView 的工作原理有误解。获得将用于数据源(UICollectionViewDataSource 协议)的数据后,除非支持数据已更改,否则不要重新加载它。

根据您所说的内容,我认为您的代码应该是这样的示例。请注意:我不知道如何处理 imageName,因为您总是在创建后立即分配它,然后针对 nil 对其进行测试,根据您的代码,这永远不会是真的。因此,我删除了那个位。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self runQuery];
}
- (void)runQuery {
    PFQuery *query = [PFQuery queryWithClassName:@"SwapPost"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        // You obviously need better error handling in here...
        self.dataArray = objects;
        [self.collectionView reloadData];
    }
     ];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    // This is an assumption
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CMFGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CMFGalleryCell *cell    = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    PFObject *post          = self.dataArray[indexPath.row];
    PFFile *file            = post[@"image"];
    cell.imageView.file     = file;
    [cell.imageView loadInBackground];
    return cell;
}

现在,这不是完整的代码。它会工作,但它没有优化并且它没有错误处理以防你没有从 Parse 得到任何返回(或得到错误)。此外,根据查询需要多长时间,您的 UI 将不会显示任何内容以向用户表明正在取得进展。我通常使用 MBProgressHUD 的实例来阻止 UI 并指示正在进行查询。抛开这些注意事项,这应该可以帮助您入门。