I added UICollectionView by code.
Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter.
Do you have any idea to fix it?
CollectionCell is custom class of UICollectionViewCell.

@property (nonatomic, strong) UICollectionView* collectionView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView = [[UICollectionView alloc]init];
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell"];
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 100);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:flowLayout];

    self.collectionView.frame = CGRectMake(0, 60, 320, 500);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.eventCollection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell* cell = [self.eventCollection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.label.text = [NSString stringWithFormat:@"%d", indexPath.item];
    return cell;
}

推荐答案

这次坠机非常明确地告诉你出了什么问题:

UICollectionView必须使用非nil布局参数初始化.

如果您判断the documentation for UICollectionView,您会发现唯一的初始值设定项是initWithFrame:collectionViewLayout:.此外,在该初始化式的参数中,您将看到:

frame

集合视图的框架矩形,以点为单位.框架的原点相对于您计划在其中添加它的SuperView.该帧在初始化期间被传递给超类.

layout

用于组织项的Layout对象.集合视图存储对指定对象的强引用.Must not be nil.

我有bolded个最重要的部分.您必须使用initWithFrame:collectionViewLayout:来初始化UICollectionView,并且必须向其传递一个非nil UICollectionViewLayout对象.


那么,解决这个问题的一种方法就是简单地更改初始化的顺序:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

请注意,在上面的示例中,我假设您希望使用self.view.frame作为self.collectionView的帧.如果不是这样,请插入任何您想要的框架.

Ios相关问答推荐

Flutter WebRTC:GetUserMedia()不适用于Safari和IOS设备

如何在@AppStorage中存储字体?

多协议和类继承混乱,Swift

为什么在Actor内部使用withTaskGroupwork并行运行?

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

Swift Combine:prepend() before share() 阻止初始接收器执行

iOS应用程序崩溃,必需条件为false:isformatsamplerateandchannelcountvalid(format)

如何根据条件快速将返回类型设为 LinearGradient 或 AngularGradient?

Text() 正在添加额外的前导尾随填充 SwiftUI

iPhone 6 和 6 Plus 媒体查询

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

iOS 10 / Xcode 8 设备上的 NSLog 似乎被截断了?为什么?

在 iOS 7 上更改标签栏色调 colored颜色

直接下载Xcode模拟器

将自定义对象保存到 NSUserDefaults

使用 Xcode 4 的 iPhone 临时构建

如何在 iOS 中获取正在运行的应用程序的名称

UIView - 加载视图时如何获得通知?

如何使用 presentModalViewController 创建透明视图

使用 xib 创建可重用的 UIView(并从情节提要中加载)