我想使用UICollectionViewCell
,因爲我想顯示的只是一個圖像。我可以在UICollectionViewCell
的contentView
屬性上使用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
。那是因爲我想改變視圖的背景顏色。我不確定問題是什麼,但有人可以看看我的代碼並幫助我嗎?我一直試圖弄清楚這一點,絕對沒有運氣。
謝謝!驅使我瘋了:) –
如果你想使用Xib,你可以在這個文檔中找到答案[UICollectionView添加UICollectionCell](http://stackoverflow.com/questions/15184968/uicollectionview-adding-uicollectioncell)。 – Autobots