我在情节提要中有一个具有4个集合视图单元原型的集合视图,这些原型具有以下标识符:“单元”,“ NoAlbumSelectedCell”,“ NoPhotosCell”和“ NoVideosCell”。
这是我用来加载单元格的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (self.noAlbumSelected) {
return [collectionView dequeueReusableCellWithReuseIdentifier:@"NoAlbumSelectedCell" forIndexPath:indexPath];
}
if ([self.medias count] == 0) {
if ([self.listType isEqualToString:@"photo"]) {
UICollectionViewCell *noPhotosCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NoPhotosCell" forIndexPath:indexPath];
return noPhotosCell;
} else {
UICollectionViewCell *noVideosCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NoVideosCell" forIndexPath:indexPath];
return noVideosCell;
}
}
MediaCollectionViewCell *cell = (MediaCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell forItemAtIndexPath:indexPath];
return cell;
}
当我尝试显示“ NoPhotosCell”单元格时,出现以下错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier NoPhotosCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我试图进行清洁和重建。
最佳答案
您是否在viewController的viewDidLoad的末尾添加了以下几行?
[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"NoAlbumSelectedCell"];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"NoPhotosCell"];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"NoVideosCell"];
如果您有子类UICollectionViewCell,则应将
[YourUICollectionViewSubClass class]
与上述各行结合使用。