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

标签 ios uicollectionview

我通过代码添加了UICollectionView
现在,应用程序崩溃并显示消息:UICollectionView 必须使用非零布局参数进行初始化
你有什么办法解决这个问题吗?
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 must be initialized with a non-nil layout parameter.

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

frame

The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.

layout

The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.

我已将重要部分加粗。您必须使用 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 的框架。如果不是这种情况,请插入您想要的任何框架。

关于ios - UICollectionView:必须使用非零布局参数进行初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48408619/

相关文章:

ios - 处理完openUrl请求后返回sourceApplication

ios - 如何使用 IMvxMessenger 对 MvvmCross 进行单元测试

ios - NSTimer - 如何在 Swift 中延迟

swift - 修改 uicollectionview 中的特定单元格

ios - 如何检查哪个collectionview是否在快速滚动

ios - UICollectionView 自定义布局

ios - 如何添加 UIPickerView 部分?

ios - 将外部音轨添加到 HLS 视频

ios - 如何在 UICollectionView 中查找与特定单元格水平和垂直相邻的单元格/索引路径

ios - UICollectionViewCell 中 UILabel 的 UIView 动画 block 不起作用