1
前言:我的应用程序的iPad布局需要在NSFetchedResultsController提供的项目(“PostCell”)开始处的集合视图内呈现单元格(“PostAddCell”)。该单元格用作创建新项目的按钮。在iPhone上这不是必需的,因为有一个用于添加新帖子的UI按钮。UICollectionView不显示单元格
问题:仅当NSFetchedResultsController没有结果时才会出现PostAddCell。如果获取收益7,所有我看到的是7个PostCells即使断点和记录表明是由数据源0返回PostAddCell索引路径:0
#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;
}
我会想在每一节的第0行MNPAddPostCell。我不明白PostCell会如何跺跺PostAddCell标识符。外部块是iPad和其他。内部是仅用于iPad的行检查,并且创建newIndexPath以确保单元格创建不尝试访问不存在的行。 –
那么,您为第1行返回cellForItemAtIndexPath的新单元格的indexPath将用于第0行,因此我不知道UITableView是否使用它,而不是它最初传递的indexPath,这就是我踩踏PostAddCell的意思。 –
s/UITableView/UICollectionView :) –