2014-04-04 56 views
23

我有一個函數來更新現有的UICollectionView。 UICollectionView被創建,我可以看到它,但是當我想訪問它的單元格來更新它時,它們是零。UICollectionView cellForItemAtIndexPath是零

-(void)finishExam{ 

    for (int i = 0; i < [self.questionsOverviewCollection numberOfItemsInSection:0]; i++) { 

     NSLog(@"self.questionsOverviewCollection - %@",self.questionsOverviewCollection); 
     NSLog(@"cell - %@",[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]); 
     NSLog(@"overviewCell - %@",(OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]); 
     NSLog(@"numOfCells - %d", [self.questionsOverviewCollection numberOfItemsInSection:0]); 

     OverviewCell *cell = (OverviewCell*)[self.questionsOverviewCollection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
      [cell finishExam]; 
    } 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    OverviewCell *cell = (OverviewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    [cell someSetUp]; 

    return cell; 
} 

登錄:

self.questionsOverviewCollection - <UICollectionView: 0xa1abc00; frame = (14 219; 217 441); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0xe0617a0>; layer = <CALayer: 0xe0bbb00>; contentOffset: {0, 0}> collection view layout: <UICollectionViewFlowLayout: 0xe0cc3f0> 
cell - (null) 
overviewCell - (null) 
numOfCells - 30 
+0

你得到所有小區零或你得到一些細胞,之後u得到空? – CRDave

回答

42

UICollectionView文檔(強調我自己)

返回值
在相應的索引路徑Cell對象或零如果單元格不可見或indexPath超出範圍。

您應該更新您的基礎模型,它將數據提供給視圖。

+9

這對我來說很合適,但除了使用[view reloadData]重新加載數據外,我還必須按照http://stackoverflow.com/a/21480786/1388195 – zyzof

+19

調用[collectionView layoutIfNeeded]工作的[查看layoutIfNeeded]我在調用[collectionView reloadData]之後獲取單元格。謝謝@zyzof –

+3

感謝所有的layoutIfNeeded !!! :D只是想知道,有人知道爲什麼嗎?因爲在我的情況下,單元格一直可見。 – Happiehappie

6

試試這個:

[collectionView reloadData]; 
[collectionView layoutIfNeeded]; 
相關問題