Ios UICollectionView,网络数据在与交互之前不显示单元格
我有一个加载了*.xib的UIViewController。此ViewController的一个元素是UICollectionView。我在ViewControllers-viewDidLoad中发出异步网络请求,并在回调时调用self.collectionView重载数据。我还尝试:Ios UICollectionView,网络数据在与交互之前不显示单元格,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有一个加载了*.xib的UIViewController。此ViewController的一个元素是UICollectionView。我在ViewControllers-viewDidLoad中发出异步网络请求,并在回调时调用self.collectionView重载数据。我还尝试: dispatch_async(dispatch_get_main_queue(), ^{ [self.collectionView reloadData]; }); 奇怪的是,collectionVie
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
奇怪的是,collectionView没有称重传感器,只是没有在屏幕上显示而不需要交互的传感器。滚动后,滚动到视图中的单元格显示良好。当我滚动回到开始时,第一个单元格也会出现
我错过了什么
我的数据源方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.listItems count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:9];
if (!lbl) {
UILabel *lbl = [[UILabel alloc] initWithFrame:cell.contentView.frame];
[lbl setTag:9];
[cell.contentView addSubview:lbl];
}
[lbl setBackgroundColor:[UIColor colorWithRed:1.f green:1.f blue:(10*indexPath.row)/255.f alpha:1]];
[lbl setText:[NSString stringWithFormat:@"%d", indexPath.row]];
return cell;
}
在回调时,我只需设置self.listItems=myFetchedArray并在collectionView上调用-reloadData…尝试在重新加载数据后更新collectionView布局
目标-C
[self.collectionView.collectionViewLayout invalidateLayout]
敏捷的
collectionView.collectionViewLayout.invalidateLayout
您可以随时调用此方法来更新布局信息。
此方法使集合视图本身的布局无效,并且
马上回来。因此,您可以从中多次调用此方法
不触发多个布局更新的相同代码块。这个
实际布局更新发生在下一个视图布局更新周期中
数据源方法中有哪些内容?在提取完成后,您如何处理数据?没有这些细节,这个问题毫无意义。@nhgrif补充道。抱歉。我认为这没有帮助,但在您调用[self.collectionView reloadData];,您是否也可以尝试添加[self.collectionView.collectionViewLayout invalidateLayout];之后?@opfeffer你修好了吗?我有一个非常类似的问题,我对你的意见很感兴趣: