2014-02-14 30 views
2

我有一个非常简单的收集视图,但不知何故它不断崩溃。以下是错误消息:错误与UICollectionView

终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“不能出列类型的视图:UICollectionElementKindCell与标识符再利用 - 必须注册标识的笔尖或一类或连接在故事板”

和我的代码的原型细胞:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *reuseCellIdentifier = @"Reuse"; 

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

    if (!cell) { 
     cell = [[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 106, 95)]; 
    } 

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)]; 

    imageView.image = [UIImage imageNamed:@"image_name"]; 
    [cell addSubview:imageView]; 

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 106, 15)]; 
    name.text = @"user"; 

    return cell; 
} 

帮助!

+0

给小区标识为“再利用”内收集可重复使用的视图部分。 – cjd

回答

2

好像你没有申报在你的故事板的细胞再利用标识,做这些步骤:

- 选择你的故事板细胞

enter image description here

- 将重用标识符输入到“Collection Reusable view identifier”中,在您的情况下在其中输入“重用”

enter image description here

希望帮助..

2

使用这个,如果没有设置委托。请您的代表。

在调用集合视图的dequeueReusableCellWithReuseIdentifier:forIndexPath:方法之前,必须使用此方法或registerNib:forCellWithReuseIdentifier:方法告诉集合视图如何创建给定类型的新单元格。如果指定类型的单元当前不在重用队列中,则集合视图将使用提供的信息自动创建新的单元对象。

[self.yourcollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; 

enter image description here

apple doc

相关问题