UICollectionView不是registring单元格

时间:2014-02-15 08:05:32

标签: ios crash uicollectionview uicollectionviewcell

我在同一个控制器中使用了两个UICollectionView,但是当我初始化第二个UICollectionView时,它会像

一样给予异常

由于未捕获的异常NSInternalInconsistencyException而终止应用,原因: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

这是我的代码:

- (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;

}

2 个答案:

答案 0 :(得分:1)

请在UICollectionViewCell注册viewDidLoad课程 像:

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

答案 1 :(得分:0)

将您的单元格标识符设置为代码

enter image description here