繁体   English   中英

调用reloadData时,UICollectionView不会立即更新,而是在30-60秒后随机更新

[英]UICollectionView doesn't update immediately when calling reloadData, but randomly after 30-60 seconds

正如标题所暗示的那样,我的UICollectionView在调用reloadData后不会立即更新并显示单元格。 相反,它似乎最终在30-60秒后更新我的集合视图。 我的设置如下:

UICollectionView加入与两个故事板,以查看控制器delegatedataSource设置为视图控制器和标准出口设置numberOfSectionsInRowcellForItemAtIndexPath都实现并引用试制的电池和imageView在它的内部

以下是转到Twitter的代码,获取时间轴,将其分配给变量,使用推文重新加载表格视图,然后通过推文查找照片并使用这些项目重新加载集合视图。

即使我注释掉显示图像的代码,它仍然不会改变任何东西。

SLRequest *timelineRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:timelineURL parameters:timelineParams];
[timelineRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if(responseData) {
        JSONDecoder *decoder = [[JSONDecoder alloc] init];

        NSArray *timeline = [decoder objectWithData:responseData];

        [self setTwitterTableData:timeline];

        for(NSDictionary *tweet in [self twitterTableData]) {
            if(![tweet valueForKeyPath:@"entities.media"]) { continue; }

            for(NSDictionary *photo in [[tweet objectForKey:@"entities"] objectForKey:@"media"]) {
                [[self photoStreamArray] addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                                    [photo objectForKey:@"media_url"], @"url",
                                                    [NSValue valueWithCGSize:CGSizeMake([[photo valueForKeyPath:@"sizes.large.w"] floatValue], [[photo valueForKeyPath:@"sizes.large.h"] floatValue])], @"size"
                                                    , nil]];
            }
        }

        [[self photoStreamCollectionView] reloadData];
    }
}];

这是从后台线程调用UIKit方法的经典症状。 如果你查看-[SLRequest performRequestWithHandler:]文档 ,它说处理程序不保证它将在哪个线程上运行。

在块中包含对reloadData的调用并将其传递给dispatch_async() ; 还将dispatch_get_main_queue()作为队列参数传递。

您需要将更新分派给主线程:

 dispatch_async(dispatch_get_main_queue(), ^{
    [self.photoStreamCollectionView reloadData];
  });

或者在Swift中:

dispatch_async(dispatch_get_main_queue(), {
    self.photoStreamCollectionView.reloadData()
})

Apple说:你不应该在插入或删除项目的动画块中间调用此方法。 插入和删除会自动使表的数据得到适当更新。

面对:你不应该在任何动画的中间调用这个方法(在滚动中包含UICollectionView)。

所以你可以:

[self.collectionView setContentOffset:CGPointZero animated:NO];
[self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

或者确定没有任何动画,然后调用reloadData; 要么

[self.collectionView performBatchUpdates:^{
//insert, delete, reload, or move operations
} completion:nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM