2012-09-26 91 views
25

,我發現了錯誤...的iOS斷言失敗在UICollectionView

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249 

當試圖顯示UICollectionView。

導致它的線......在出隊

static NSString *CellIdentifier = @"Cell"; 

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

錯誤發生。

有沒有其他的錯誤,所以我很難知道從哪裏開始。

任何人都可以闡明這一點嗎?

回答

27

在閱讀的文檔(應該可能已經這樣做了第一:))

無論如何,我使用的CollectionView是一個單獨的廈門國際銀行內文件(不是故事板)和文檔...

Important: You must register a class or nib file using the 
registerClass:forCellWithReuseIdentifier: or 
registerNib:forCellWithReuseIdentifier: method before calling this method. 

感謝

+0

你什麼時候調用這個方法?在「UICollectionViewCell * cell = [collectionView dequeue ...」之前的行或在不同的方法 –

+1

我必須註冊它在viewDidLoad方法。您只需要爲整個collectionView註冊一次xib。然後,當您調用dequeueCellWithIdentifier時,它會轉到您註冊的xib。 – Fogmeister

+2

我有同樣的問題,但由於某種奇怪的原因,編譯器無法識別'viewDidLoad'中的'registerClass'方法,所以我不得不將它移動到'cellForItemAtIndexPath'方法。 –

0

更換

NSString *CellIdentifier = @"Cell"; 

static NSString *CellIdentifier = @"Cell"; 
+0

先試過。我剛剛閱讀文檔。我需要在運行出隊前註冊一個班級或筆尖... – Fogmeister

36

你需要象下面這樣註冊:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"]; 
3

確保如果使用registerNib:方法:

UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil]; 
[collectionView registerNib:nibH 
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
     withReuseIdentifier:HEADER_ID]; 

ALSO在筆尖文件,當你選擇頂級集合可重用視圖,使用屬性檢查器,確保Identifier設置爲您傳遞給withReuseIdentifier:參數的值相同。

0

我看到這個錯誤彈出時,使用獨特的ReuseIdentifiers多個UICollectionViews。在viewDidLoad中要註冊,像這樣各的CollectionView的reuseIdentifier:當你到

[_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"]; 
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"]; 

然後 「 - (UICollectionViewCell *)的CollectionView:(UICollectionView *)的CollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath」 你要確保您不會嘗試將collectionView1的單元格設置爲collectionView2的reuseIdentifier,否則會出現此錯誤。

千萬不要這麼做:(或collectionView2會看到錯誤的標識,看標識就期待前大發脾氣)

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath]; 

if(collectionView != _collectionView1){ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath]; 
} 

cell.backgroundColor = [UIColor greenColor]; 
return cell; 

做到這一點

UICollectionViewCell *cell; 

if(collectionView == _collectionView1){ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath]; 
}else{ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath]; 
} 

cell.backgroundColor = [UIColor greenColor]; 
return cell; 
1

我有同樣的問題。這是我解決它的方法。

移動

[self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

是在- (void)viewDidLoad

而非方法- (void)awakeFromNib