OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView 出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:27
标题: ios - UICollectionView 出错

我有一个非常简单的 Collection View ,但不知何故它总是崩溃。这是错误消息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Reuse - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

还有我的代码:

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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;
}

救命!!



Best Answer-推荐答案


似乎您没有在 Storyboard 上声明单元重用标识符,请执行以下步骤:

- 选择 Storyboard上的单元格

enter image description here

- 在“Collection Reusable view identifier”中输入重用标识符,在您的情况下,在此处输入“Reuse”

enter image description here

希望对您有所帮助..

关于ios - UICollectionView 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21772652/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4