2013-11-09 200 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 :) –

相關問題