gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:50:11 25 4
gpt4 key购买 nike

我通过代码添加了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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com