前言:我的应用程序的 iPad 布局需要在 NSFetchedResultsController 提供的项目(“PostCell”)开始处的集合视图中呈现一个单元格(“PostAddCell”)。此单元格用作创建新项目的按钮。在 iPhone 上,这不是必需的,因为有用于添加新帖子的 UI 按钮。
问题: PostAddCell 仅在 NSFetchedResultsController 没有结果时出现。如果 fetch 返回 7,我看到的只是 7 个 PostCell,即使断点和日志记录表明索引路径 0:0 的数据源正在返回 PostAddCell
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [_fetch sections][section];
if(isPad()){
return [sectionInfo numberOfObjects]+1;
}else{
return [sectionInfo numberOfObjects];
}
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return [[_fetch sections] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell;
if(isPad()){
if(indexPath.row == 0){
cell = [cv dequeueReusableCellWithReuseIdentifier:@"PostAddCell" forIndexPath:indexPath];
[(MNPAddPostCell*)cell configure];
}else{
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section];
cell = [cv dequeueReusableCellWithReuseIdentifier:@"PostCell" forIndexPath:newIndexPath];
[(MNPPostCell*)cell configureForPost:[_fetch objectAtIndexPath:newIndexPath]];
}
}else{
cell = [cv dequeueReusableCellWithReuseIdentifier:@"PostCell" forIndexPath:indexPath];
[(MNPPostCell*)cell configureForPost:[_fetch objectAtIndexPath:indexPath]];
}
return cell;
}