ios - 错误 : "could not dequeue a view of kind" in Objective-C

标签 ios objective-c xcode uicollectionview

我在 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/

相关文章:

ios - 如何将 Voiceover 绑定(bind)到 MPVolumeView slider 中?

objective-c - iOS Core Data 如何使用谓词正确比较字符串文本?

ios - 我的应用程序是否显示第二次通知 iOS 9

xcode - '(' 之前的预期标识符 'Synthesize"

ios - 如何在 Xcode 中捕获代码指标?

ios - 如何使用 appendFormat 在 Swift 中格式化字符串?

android - 在 Android 中使用它的类似 UICollectionViewDataSource 方法?

ios - 为什么我的谷歌登录在我成功登录后不显示帐户选择?

iOS - 将点从一个坐标系转换为另一个坐标系

ios - objc_msgSend EXC_BAD_ACCESS 使用关闭按钮时