ios - 解析 UICollectionView 的查询不工作

标签 ios objective-c parse-platform uicollectionview

目前,我正在尝试通过 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 协议(protocol)),除非支持数据已更改,否则您不会重新加载它。

根据您的陈述,这是我认为您的代码应该是什么样子的示例。请注意:我不知道如何处理 imageName,因为您总是在创建时立即分配它,然后测试它是否为 nil,这永远不会是基于 true 的在你的代码上。因此,我删除了那部分。

- (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 并指示正在进行查询。抛开这些注意事项,这应该可以帮助您入门。

关于ios - 解析 UICollectionView 的查询不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27833890/

相关文章:

iphone - 计算每 N 个字符的换行符?

iOS 配置 : How do I discard unwanted app ids and provisioning profiles?

ios - iOS 7 和 iOS 8 的人物选择器

ios - 为什么 UISearchBar tableview 索引路径总是返回 objectAtIndex :0

iphone - UITableView 中的点击事件

javascript - 为什么我的用户登录逻辑不起作用?

ios - 长时间运行的解析操作正在主线程上执行似乎无法摆脱这个

ios - 无线更新 .stringsdict 本地化文件

ios - 将 token 发送到后端 strip objective-c

swift - 上传多个图像进行解析