错误:Objective-C 中的“无法使同类视图出队”
Posted
技术标签:
【中文标题】错误:Objective-C 中的“无法使同类视图出队”【英文标题】:Error : "could not dequeue a view of kind" in Objective-C 【发布时间】:2017-07-13 08:59:40 【问题描述】:我在 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中reuseIdentifier的名字是什么? @hariszaman myID. 然后更改静态 NSString * const reuseIdentifier = @"Cell"; to static NSString * const reuseIdentifier = @"myID"; 【参考方案1】:我认为您在情节提要中有另一个 UICollectionView,但单元格标识符为空
【讨论】:
不,我有单元格标识符。【参考方案2】:在创建collection view
之后添加self.collectionView = collectionview;
。
【讨论】:
【参考方案3】:您的崩溃提供了与您注册的不同的类:
UICollectionElementKindCell
对比
UICollectionViewCell
将其中一个更改为您想要的课程 - 我猜是后者。
【讨论】:
以上是关于错误:Objective-C 中的“无法使同类视图出队”的主要内容,如果未能解决你的问题,请参考以下文章
Objective-C 中的“无法识别的选择器发送到实例”错误
由于 Objective-C 中的整数溢出,处理和报告内存分配错误的最佳方法是啥?