gpt4 book ai didi

ios - 在 Nib 文件中添加 UICollectionView 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 01:16:23 24 4
gpt4 key购买 nike

我正在使用 HMSegmentedControl 框架构建一个实验应用程序。在此应用程序上,我尝试将 UICollection View 添加到 xib 文件上创建的 View 中,但我遇到了问题。应用程序崩溃了,我得到这个:

2016-02-01 10:23:40.693 SegmentControlExperimentation[1328:49671] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UICollectionView.m:3690
2016-02-01 10:23:40.699 SegmentControlExperimentation[1328:49671] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我到处查找,但找不到解决方案。所以我创建了一个 github 存储库,并在此链接上克隆了我的代码:https://github.com/nascimentorafael/SegmentControlExperimentation这是一个非常小且简单的代码。如果运行它,就会发生这种崩溃。因此,请查看 ViewController 类和 view.xib。

我希望有人能帮助我。

最佳答案

我浏览了你的 github 存储库,发现你没有以正确的方式练习 UICollectionView。但是,如果您只想避免崩溃,只需在 ViewController.m 中注释掉第 102-105 行和第 128 行的代码

如果你想把你的 collectionView 放在 view.xib 中,你需要在 view.h 中插入这一行

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

在 view.xib 中引用上面的 outlet 并在 ViewController.m 中注释掉第 35-37 行

然后在 - (void)viewDidLoad {} 中为 View *view 添加如下代码



UINib *cellNib = [UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil];
[view.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
view.collectionView.delegate = self;
view.collectionView.dataSource = self;

仍然请理解,以上建议是对您当前存储库的最小更改,可能不是最佳实践。需要考虑的事情很多,根据不同的场景,组合也会有很大的不同。

关于ios - 在 Nib 文件中添加 UICollectionView 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132097/

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