我在一个故事板中有一个集合视图,其中有4个集合视图单元原型,具有以下标识符:" cell ", "NoAlbumSelectedCell", " nophotocell "one_answers"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;
}
当我尝试显示"nophotocell"单元格时,我得到以下错误:
*** 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]
与上述行结合使用。
您在viewdidload
中编写了以下代码// Do any additional setup after loading the view from its nib.
[self.collectionView registerClass:[CollectionViewImageCell class] forCellWithReuseIdentifier:@"Cell"];