我有一个简单的 UICollectionView。第一次加载 View 时,cellForItemAtIndexPath 被调用了三次,我看到了三个单元格。
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
NSUInteger count = ([self.results count] > 0 ? [self.results count] : 3);
NSLog(@"count %lu", (unsigned long)count);
return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForItemAtIndexPath called");
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"TasteCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
但是,后来 self.results 变成了一个包含 5 个元素的数组,我调用:
[self.collectionView reloadData];
发生这种情况时,再次调用 numberOfItemsInSection,那里的“count”日志语句显示它返回 5。但是,这次从未调用 cellForItemAtIndexPath,UI 仍然显示第一次加载时的三个单元格。
为什么即使计数已更改,单元格的数量仍未更新?
最佳答案
我认为,您可能正在使用静态 Collection View 单元格的概念。 如果它是静态的,请在您的 xib/Storyboard 中将其更改为动态。
关于ios - UICollectionView:重新加载数据调用 numberOfItemsInSection 但不是 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339397/