【问题标题】:Refresh control with UICollectionView reloadData issue使用 UICollectionView reloadData 问题刷新控件
【发布时间】:2013-07-07 17:57:47
【问题描述】:

我继续使用 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 提供的控件。

您是否发现可能导致此行为的原因?

【问题讨论】:

  • 你的数据源在哪里,你更新了吗?

标签: ios uicollectionview parse-platform reloaddata uirefreshcontrol


【解决方案1】:

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

- (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`

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

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 2014-04-10
    • 2021-12-09
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多