PFQuery 在 UICollectionview 中多次返回对象

PFQuery returning object multiple times in UICollectionview

我创建了一个 UICollection 视图,并使 collectionviewcells 显示缩略图。后端有四个对象。集合视图检索四个对象,但是,它通常不止一次检索一个或多个对象,而不是只查询和显示四个对象中的每一个对象一次。我不确定可能是什么问题。这是我查询数据并将其显示在集合视图中的方法:

-(void)queryForTable {
    NSLog(@"Querying");
    PFQuery *query = [PFQuery queryWithClassName:@"VideoApp"];
    NSString * author = [[PFUser currentUser] objectForKey:@"FBName"];
    [query whereKey:@"author" equalTo:author];
    [query orderByAscending:@"createdAt"];
    [query setCachePolicy:kPFCachePolicyNetworkOnly];



        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                NSLog(@"Successfully retrieved %d objects", objects.count);
                userVideosArray = objects;
                [self.collectionView reloadData];

   /*             for (PFObject *object in objects) {
                    PFFile *thumbnail = [object objectForKey:@"video_thumbnail"];
                    [thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                        if (!error) {
                            NSLog(@"Fetching image");
                            //[userVideosArray addObject:[UIImage imageWithData:data]];
                            videos = objects;
                        } else {
                            NSLog(@"Error: %@ %@", error, [error userInfo]);
                        }
                    }];
                } */
            }
        }];
    //return videos;


}

创建单元格的方法如下:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"initializing collection view");
    //CollectionViewcellCollectionViewCell * cell = (CollectionViewcellCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    CollectionViewcellCollectionViewCell *cell = (CollectionViewcellCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

   // NSLog(@"%@", videos);

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.backgroundColor = [UIColor clearColor];
   // imageView.image = [videos objectAtIndex:indexPath.item];
    [cell addSubview:imageView];


    //NSString *tmpObjectId = userVideosArray[indexPath.item];
    PFQuery *query = [PFQuery queryWithClassName:@"VideoApp"];
    NSString * author = [[PFUser currentUser] objectForKey:@"FBName"];
    [query whereKey:@"author" equalTo:author];
    [query orderByAscending:@"createdAt"];
    NSLog(@"AUTHOR IS: %@", author);
    NSLog(@"number of objects in query: %i", [query countObjects]);

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        //tagTxt.text = [NSString stringWithFormat:@"%@", [object objectForKey:@"video_tag"]];
        //handleTxt.text = [NSString stringWithFormat:@"By: %@", [object objectForKey:@"author"]];
        // locTxt.text = [NSString stringWithFormat:@"From: %@",[object objectForKey:@"video_location"]];
        for (PFObject *object in objects) {

            PFFile *imageFile = [object objectForKey:@"video_thumbnail"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
                if (!error) {
                    imageView.image = [UIImage imageWithData:data];
                    //[self.collectionView reloadData];

                }
            }];

        }

    }];


    return cell;
}

您永远不应从视图的委托方法中获取 and/or 创建数据源。视图应该只反映它所代表的模态部分。

您面临的问题是,图像是在后台获取的,当通过单元格获取查询时,您可能会在位置 1 看到该单元格。但是由于集合视图重用该单元格,它将使用向下滚动时再次显示相同的单元格。之前获取的查询可能最终会在此时执行; IE。图像将分配给单元格。所以即使此时发送了新的请求,再次显示也需要时间。

另一件事是,在 'cellForItem..' 方法中向单元格添加子视图并不好,因为同一个单元格将一次又一次地添加更多子视图。

作为上述两者的破解方法,您可以在每次出队时删除单元格的所有子视图。这样即使执行了先前的查询,也不会分配图像。