【发布时间】:2014-03-11 20:06:09
【问题描述】:
当我的应用程序启动时,它会从持久性中获取数据并在我的集合视图中显示 5 个单元格。这些单元格看起来像普通的 TableView 单元格,但我使用 Collection View 来进行以后的布局调整。填满整个屏幕需要 8 个单元格。
此后一秒,模型更新为 19 个单元格的数据,集合视图通过调用更新:
[self.collectionView reloadData];
已经显示的 5 个单元格用新数据很好地刷新了,仅此而已。在我触摸屏幕并向下滚动单元格的高度之前,不会显示其他单元格。然后立即显示 3 个单元格并填满屏幕。从那一刻起,一切都很好,但在我滚动之前,它没有正确更新。
当我调试它时,我看到,collectionView:numberOfItemsInSection: 在调用 reloadData 后被调用,它返回正确的数字 19,但 collectionView:cellForItemAtIndexPath: 只询问这 5 个单元格(直到我滚动)。
这里有点绝望,谢谢你的帮助。
编辑 - 添加源代码:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.manager.jubilees count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DLog(@"asked for cell: %i", indexPath.row); //just for debugging to know, what is going on
JUJubileeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JubileeCell"
forIndexPath:indexPath];
cell.jubilee = self.manager.jubilees[(NSUInteger) indexPath.row];
return cell;
}
// called when model is updated
-(void)jubileesUpdated {
DLog(@"jubileesUpdated");
[self.collectionView reloadData];
}
编辑 2: 加载前 5 个单元格时,[self.collectionView contentSize] 为 (width=320, height=358)。 当加载所有 19 个单元格并调用 reloadData 时,contentSize 为 (width=320, height=1366) - 但单元格实际上并未显示:(。
编辑 3: 问题是由使用 THStringyFlowLayout 引起的 - 如果我找到原因,我会发布更多。
【问题讨论】:
-
如果您不滚动但等待一分钟左右会发生什么?可能与线程有关。延迟出现的单元格是从不是主线程的线程进行调用的症状。因此,请确保从 mainThread 调用 reloadData。例如,在
reloadData之前添加NSAssert([NSThread isMainThread], @"not on main thread");。这基本上是我能想到的唯一问题。 -
这点很好,但不幸的是它在主线程上更新了:(。我在开始处理这个问题时已经关闭了所有多线程。
标签: ios objective-c uicollectionview