ios - UICollectionView:重新加载数据调用 numberOfItemsInSection 但不是 cellForItemAtIndexPath

标签 ios objective-c uicollectionview

我有一个简单的 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/

相关文章:

ios - 在 swift、iOS playground 中读取文件

objective-c - CoreAnimation 的主题

iphone - 应用程序空闲超时 - 想要在特定的空闲间隔内实现自动锁定

ios - UICollectionViewCompositionalLayout 不正确的项目大小

ios - 从 nib 自定义 UiTableViewCell 而不重用?

ios - 动画按钮 360 顺时针旋转和反向旋转

ios - 每5个单元格更改一次单元格背景

ios - 缩放和裁剪图像 ios

ios - 旋转后获取正确的可读ContentGuide

ios - reloadItemsAtIndexPaths 上的 Collection View 崩溃