Ios 使用dequeueReusableCellWithReuseIdentifer时出错
我在故事板中有一个集合视图,其中有4个集合视图单元原型,具有以下标识符:“单元”、“NoAlbumSelectedCell”、“NophotoCell”和“NovidesCell” 这是我用来加载单元格的代码:Ios 使用dequeueReusableCellWithReuseIdentifer时出错,ios,uicollectionview,uicollectionviewcell,uicollectionreusableview,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionreusableview,我在故事板中有一个集合视图,其中有4个集合视图单元原型,具有以下标识符:“单元”、“NoAlbumSelectedCell”、“NophotoCell”和“NovidesCell” 这是我用来加载单元格的代码: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (
- (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]
与上述行结合使用 您已经在viewdidload中编写了以下代码
// Do any additional setup after loading the view from its nib.
[self.collectionView registerClass:[CollectionViewImageCell class] forCellWithReuseIdentifier:@"Cell"];
这起作用了,但现在它无法拾取单元格中的内容@pedroremedios更改了第一行以匹配我在代码示例中看到的内容。