ios - UICollectionView 未注册单元格

标签 ios crash uicollectionview uicollectionviewcell

我在同一个 Controller 中使用了两个 UICollectionView 但是当我初始化第二个 UICollectionView 时它给出了类似

的异常

由于未捕获的异常 NSInternalInconsistencyException 而终止应用程序,原因:无法使类型的 View 出队:具有标识符单元格的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接一个原型(prototype) Storyboard中的单元格

这是我的代码:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView==collView) {
    return arrDatabase.count;
}
else
    return arrSearchResult.count;
}    

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:     
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier=@"cell";

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

if (collectionView==collView) {
    objprop=[arrDatabase objectAtIndex:indexPath.row];
    UIImageView *imgView=(UIImageView*)[cell viewWithTag:100];
    //imgRecipe.image=[UIImage imageNamed:[ objectAtIndex:indexPath.row]];
    imgView.image=[UIImage imageNamed:@"Hamburger.jpg"];
    UILabel *lblName=(UILabel*)[cell viewWithTag:101];
    lblName.text=[NSString stringWithFormat:@"%@",objprop.categoryName];
    //cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
}

if (collectionView==collView1) {
    NSString *str=@"setIdentifier";
    UIImageView *imgView2;
    UILabel *lblCat;
    UICollectionViewCell *cell = [collectionView
                                  dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    if (cell==nil) {
        cell
        =[[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 97, 101)];
        imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(2, 13, 92, 76)];

        lblCat=[[UILabel alloc]initWithFrame:CGRectMake(3, 94, 92, 13)];
    }

    imgView2.image=[UIImage imageNamed:@"hamburger.jpg"];
    [cell.contentView addSubview:imgView2];
    lblCat.backgroundColor=[UIColor clearColor];
    lblCat.text=[NSString stringWithFormat:@"%@",[arrSearchResult[indexPath.row] objectAtIndex:0]];
    [cell.contentView addSubview:lblCat];

}

return cell;

}

最佳答案

请在viewDidLoad中注册UICollectionViewCell类 喜欢:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

关于ios - UICollectionView 未注册单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794997/

相关文章:

ios - 获取 UICollectionView 中的列

iphone - 从 map 中用户放置的注释创建 MKPolygon

ios - 段控制位置改变

iphone - 在 iPhone 应用程序中隐藏已开发的 iPad 类

crash - Import.io 在 OS X El Capitan 上崩溃

ios - 为什么此代码在 iPhone 5 上崩溃?

c - 程序在大量输入时崩溃 (C)

ios - ios 中的全屏摄像头

ios - 在 UICollectionView 中显示错误的图像

ios - 为什么 Collection View 中的动态集合单元格不显示为给定大小,并且在 swift 3 中滚动后会发生变化