2012-09-29 41 views
29

我想使用UICollectionViewCell,因爲我想顯示的只是一個圖像。我可以在UICollectionViewCellcontentView屬性上使用UIColor colorWithImage:將圖像添加到單元格。UICollectionView cellForItemAtIndexPath不註冊單元格

在我loadView方法,我註冊了電池如下: [self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];

下面是我的cellForItemAtIndexPath方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 
    // cell customization 
    return cell; 
} 

當我運行它,只要它擊中離隊線,它崩潰與以下錯誤:

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:] 

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 

我累了設置自定義單元格,並使用我作爲班級,我得到了同樣的錯誤。我的自定義單元格子類UICollectionViewCell並沒有實現,除了默認initWithFrame。那是因爲我想改變視圖的背景顏色。我不確定問題是什麼,但有人可以看看我的代碼並幫助我嗎?我一直試圖弄清楚這一點,絕對沒有運氣。

回答

60

如果您只想顯示圖像,則不需要執行任何子類化操作,則可以使用colorWithPatternImage:設置單元的backgroundColor。註冊類是這樣的:

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

然後使用它像這樣:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]]; 
    return cell; 
} 

在這個例子中,結果是UIImagesarray

8

嘗試在

[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"]; 

設置斷點我猜你的loadView(你的意思是viewDidLoad中?)方法不會被調用,因此該類不會與註冊的CollectionView。

6

,如果你的集合視圖連接在故事板和委託和數據源設置那裏,你提供的數據源和委託必要的方法,然後將寄存器呼叫使

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

回報UICollectionView而不是你自己的子類。所以,要麼但不是兩者。

+1

謝謝!驅使我瘋了:) –

+0

如果你想使用Xib,你可以在這個文檔中找到答案[UICollectionView添加UICollectionCell](http://stackoverflow.com/questions/15184968/uicollectionview-adding-uicollectioncell)。 – Autobots

4

設置你的小區標識名稱作爲代碼

enter image description here

+0

是的,這與註冊viewDidLoad中的集合視圖做了訣竅。我確實設法在出場電話中引起了一些傷心。我有「細胞」,即與一個無關的空白,這當然不是「細胞」相同,所以要非常小心你的名字。 – TJA

12

如果您使用的是applivation XIB然後添加你的viewDidLoad方法如下

[self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"]; 

否則,如果你使用故事板添加以下

[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"]; 

最後一個dd這個(如果沒有)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 
return cell; 
} 

以上希望會有所幫助。

+2

謝謝,提醒我註冊「Nib」,祝你好運! – Resty

+0

非常感謝。你的第一個片段是我一直在尋找的:) – Mexx

+0

我正在使用故事板&mt自定義單元格沒有出列任何想法? –

相關問題