iphone - 控制台错误 : UICollectionView must be initialized with a non-nil layout parameter

标签 iphone objective-c xcode uicollectionview

我是 UICollectionView 的新手,我正在按照我在 Youtube 上找到的教程进行操作,但我遇到了一个我无法弄清楚的错误。

当我使用此代码运行应用时:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return 1;

    }

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return [self.array count];

    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        CollectionCell *aCell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

        aCell.title.text = self.array[indexPath.row];

        return aCell;

    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

    }

在 .h 中:

@property (strong, nonatomic) NSArray *array;

在控制台中我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

我没有使用 Storyboard,而是自定义了 CollectionView,您可以在此处看到:

image

有人知道我为什么会收到此错误吗?欢迎一切!

编辑:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

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

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setItemSize:CGSizeMake(60, 60)];
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];

    [self.collectionView setCollectionViewLayout:flow];

}

最佳答案

注册 uicollectionviewcell View 类时出错。要解决此问题,请在您的代码中添加以下行:

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

关于iphone - 控制台错误 : UICollectionView must be initialized with a non-nil layout parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270058/

相关文章:

iphone - 使用 HTTP Post 方法如何在 iphone 中制作登录页面

ios - 单例类保留 'Losing' UIImage

ios - 该应用程序没有有效的签名 Xcode 7.3

iphone - UIScrollView通知?

iphone - 启用 ARC 的 TWTweetComposeViewController 内存泄漏

iPhone SDK - 通过 UILabel 触摸 UIButton

iphone - 有没有办法从新的模态视图访问启动模态视图的 View Controller ?

iphone - 复制 iOS iTunes 下载动画

ios - 如何在 Objective c 的 UI 导航栏上更改 Font Awesome 的颜色

ios - Kiwi 单元测试在 Xcode 中从未失败