UICollectionView不显示单元格

时间:2013-11-09 20:07:52

标签: ios objective-c

前言:我的应用程序的iPad布局需要在NSFetchedResultsController提供的项目开头(“PostCell”)的集合视图中呈现单元格(“PostAddCell”)。此单元格用作创建新项目的按钮。在iPhone上没有必要,因为有一个用于添加新帖子的UI按钮。

问题:PostAddCell仅在NSFetchedResultsController没有结果时出现。如果fetch返回7,我看到的只有7个PostCells,即使断点和日志记录表明索引路径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;

}

1 个答案:

答案 0 :(得分:0)

我认为这里有几个问题。首先,您可能应该检查索引路径部分以及索引路径行,除非您希望每个部分的第0行都有MNPAddPostCell,假设有多个部分?其次我认为

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

可能会踩踏您的MNPAddPostCell。不应该是

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

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