我的应用程序中有两个视图,主视图控制器(viewcontroller.m
)和一个UICollectionView(photoListViewController.m
),发生了一些奇怪的事情,如果在主视图控制器上创建一个按钮,通过Storyboard创建PUSH,一切正常,但是当我尝试以编程方式创建推送时,我收到以下错误:
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:3241
*** 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'
我为单元格创建了一个自定义类(photoListCell),并通过storyboard连接了所有内容。
photoListCell.h
@property (weak, nonatomic) IBOutlet UIImageView *cellBackgroundImage;
@property (weak, nonatomic) IBOutlet UILabel *cellDataLabel;
@property (weak, nonatomic) IBOutlet UILabel *cellNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *cellFooterImage;
这就是我在Viewcontroller.m
上创建推送的方式-(void)goToPhotoList: {
UICollectionViewFlowLayout *cvLayout = [[UICollectionViewFlowLayout alloc]init];
[cvLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[cvLayout setItemSize:CGSizeMake(105, 105)];
PhotoListViewController *photoVC = [[PhotoListViewController alloc]initWithCollectionViewLayout:cvLayout];
photoVC.managedObjectContext = self.managedObjectContext; // Pass core data context
[self.navigationController pushViewController:photoVC animated:NO]; }
以下是我在集合视图中创建单元格的方法photoListViewController.m
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
photoListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.cellBackgroundImage.image = [UIimage imageNamed:@"Something"];
cell.cellFooterImage.alpha = 0.5;
cell.cellDataLabel.text = @"Something";
return cell; }
我该怎么办?任何帮助将不胜感激!
答案 0 :(得分:1)
假设您的集合视图单元格是在故事板中创建的,并且具有标识符" Cell",那么您的问题是您没有通过使用故事板获取PhotoListViewController的实例这一行,
PhotoListViewController *photoVC = [[PhotoListViewController alloc]initWithCollectionViewLayout:cvLayout];
相反,你应该给你的故事板控制器一个标识符(我称之为#34; PhotoListVC"对于我的例子),并像这样实例化,
PhotoListViewController *photoVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PhotoListVC"];
答案 1 :(得分:0)
我的猜测是故事板中的集合视图控制器包含一个链接到重用标识符“Cell”的单元原型。当您自己实例化视图时(而不是从故事板中),您将缺少此单元类的定义。
这是你应该做的:
注册此笔尖并将其映射到集合视图控制器中的标识符“Cell” - 如下所示:
UINib * nib = [UINib nibWithNibName:@“DatePickerTableViewCell”bundle:nil]; [self.tableView registerNib:nib forCellReuseIdentifier:@“Cell”];