我在 StoryBoard
中创建了一个 UICollectionViewController
并将其类设置为 CollectionViewController1
我有一个 ReuseIdentifier。
这是我的错误:
Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:
forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit
/UIKit-3600.8.1/UICollectionView.m:5115
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind:
UICollectionElementKindCell with identifier Cell - must register a nib or a class
for the identifier or connect a prototype cell in a storyboard'
这是我的代码:
#import "CollectionViewController1.h"
@interface CollectionViewController1 ()
{
UICollectionView *collectionview;
}
@end
@implementation CollectionViewController1
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.itemSize = self.view.frame.size;
collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionview.pagingEnabled = TRUE;
collectionview.translatesAutoresizingMaskIntoConstraints = NO;
collectionview.delegate = self;
collectionview.dataSource = self;
collectionview.bounces = false;
[collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.view addSubview:collectionview];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.view.frame.size;
}
最佳答案
我想你在 Storyboard 中有另一个单元格标识符为空的 UICollectionView
关于ios - 错误 : "could not dequeue a view of kind" in Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075980/