UICollectionView:必须使用非零布局参数进行初始化

我通过代码添加了UICollectionView
现在,应用程序崩溃的消息: UICollectionView must be initialized with a non-nil layout parameter
你有什么想法解决它?
CollectionCellUICollectionViewCell自定义类。

 @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必须使用非零布局参数进行初始化。

如果您检查Apple文档的UICollectionView ,您会发现唯一的初始化程序是initWithFrame:collectionViewLayout: UICollectionView 此外,在该初始化程序的参数中,您将看到:

采集视图的框架矩形,以点为单位。 框架的起源与您打算添加它的超级视图有关。 这个帧在初始化时被传递给超类。

布局

用于组织项目的布局对象。 集合视图存储对指定对象的强引用。 一定不能是零。

我强调了重要的一部分。 你必须使用initWithFrame:collectionViewLayout:来初始化你的UICollectionView ,并且你必须传递一个非零的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的框架。 如果不是这种情况,请插入任何你想要的框架。

只是Riley Avron答案的一个迅速版本

  let layout = UICollectionViewFlowLayout() layout.itemSize = CGSizeMake(100, 100) let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) self.view.addSubview(collectionView) collectionView.delegate = self collectionView.dataSource = self collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier") 

Swift 3.0

UICollectionView被初始化时,应用程序一旦启动就会崩溃

初始化 如下 (在viewDidLoad之上)

 let alarmCollectionView:UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init()) let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init() 

在方法 (内部viewDidLoad

 layout.scrollDirection = UICollectionViewScrollDirection.vertical alarmCollectionView.setCollectionViewLayout(layout, animated: true) alarmCollectionView.delegate = self alarmCollectionView.dataSource = self alarmCollectionView.backgroundColor = UIColor.clear self.addSubview(alarmCollectionView) 

初始化后,工作正常,像这样我使用Autolay,所以使用CGRect.zero或使用自己的CGRect