使用UICollectionView reloadData问题刷新控件

时间:2013-07-07 17:57:48

标签: ios uicollectionview parse-platform reloaddata uirefreshcontrol

我继续使用Parse改进我的UICollectionViewController的实现,这次我正在处理一个可能与缓存或者reloadData方法本身有关的问题。

也许你可以帮助我找出这种奇怪行为的来源,我最好在短视频中向你展示以节省时间:

Refreshing issue video

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

    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refershControlAction) forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];

    [self queryForTable];

}

然后在我的refreshControlAction上:

- (void)refershControlAction
{
    NSLog(@"Reload grid");

    // The user just pulled down the collection view. Start loading data.    
    [self queryForTable];

    [refreshControl endRefreshing];
}

查询方法如下:

- (void)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
    query.limit = 15;
    [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]);
        }
    }];
}

这不会发生在我的PFQueryTableViewController上,我正在执行完全相同的查询,我也在使用iOS 6刷新控件,而不是Parse提供的控件。

您是否看到可能导致此行为的事情?

1 个答案:

答案 0 :(得分:0)

我可以在你的代码中看到一些问题。

- (void)refershControlAction
{
NSLog(@"Reload grid");

// The user just pulled down the collection view. Start loading data.    
[self queryForTable];

[refreshControl endRefreshing];
} 
  在您的查询完成之前

endRefreshing,因此使用错误。查询完成时,您应该放置[refreshControl endRefreshing] in your - (voi)queryForTable`

另一个问题是我不知道在查询完成时是否更新了数据源。