2013-11-09 201 views
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

有几个我觉得这里的问题。首先,或许您应该检查索引路径部分以及索引路径行,除非您想在每个节中的第0行的MNPAddPostCell(假设有多个节)?其次,我认为

cell = [cv dequeueReusableCellWithReuseIdentifier:@"PostCell" forIndexPath:newIndexPath]; 

可能是跺脚你的MNPAddPostCell。它不应该是

cell = [cv dequeueReusableCellWithReuseIdentifier:@"PostCell" forIndexPath:indexPath]; 

而只是使用你的新索引路径从控制器获取正确的对象?

+0

我会想在每一节的第0行MNPAddPostCell。我不明白PostCell会如何跺跺PostAddCell标识符。外部块是iPad和其他。内部是仅用于iPad的行检查,并且创建newIndexPath以确保单元格创建不尝试访问不存在的行。 –

+0

那么,您为第1行返回cellForItemAtIndexPath的新单元格的indexPath将用于第0行,因此我不知道UITableView是否使用它,而不是它最初传递的indexPath,这就是我踩踏PostAddCell的意思。 –

+0

s/UITableView/UICollectionView :) –

相关问题