nul*_*fox 17 objective-c ios uicollectionview
正如标题所暗示的那样,我UICollectionView在调用后不会立即更新并显示单元格reloadData.相反,它似乎最终在30-60秒后更新我的集合视图.我的设置如下:
UICollectionView添加到查看故事板控制器与两个delegate和dataSource设置为视图控制器和标准出口设置
numberOfSectionsInRow&cellForItemAtIndexPath都实现并引用原型细胞和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];
}
}];
Run Code Online (Sandbox Code Playgroud)
Car*_*zey 42
这是从后台线程调用UIKit方法的经典症状.如果你查看-[SLRequest performRequestWithHandler:]文档,它说处理程序不保证它将运行在哪个线程上.
将你的呼叫包裹reloadData在一个区块中并将其传递给dispatch_async(); 也dispatch_get_main_queue()作为队列参数传递.
您需要将更新分派给主线程:
dispatch_async(dispatch_get_main_queue(), ^{
[self.photoStreamCollectionView reloadData];
});
Run Code Online (Sandbox Code Playgroud)
或者在Swift中:
dispatch_async(dispatch_get_main_queue(), {
self.photoStreamCollectionView.reloadData()
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17070 次 |
| 最近记录: |