加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 控制台错误:必须使用非零布局参数初始化UICollection

发布时间:2020-12-14 17:12:04 所属栏目:百科 来源:网络整理
导读:我是UICollectionView的新手,我正在按照我在Youtube上找到的教程,但我遇到了一个我无法弄清楚的错误. 当我使用此代码运行应用程序时: -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(NSInteger)collect
我是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'

我没有使用故事板,自定义了CollectionView你可以在这里看到:

有没有人有任何想法,为什么我收到此错误?一切都是受欢迎的!

编辑:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.array = @[@"First",@"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视图类时出错.要解决,请在代码中添加以下行:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读