ios - 无法出列同类视图:带有标识符的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或类
问题描述
这让我发疯。我过去已经整理了几十个集合视图,但是这个拒绝工作。
我的收藏视图单元格有一个 xib,我给它一个标识符“我的单元格”。在我的控制器中,我执行以下操作:
override func awakeFromNib() {
super.awakeFromNib()
myCollectionView.delegate = self
myCollectionView.dataSource = self
// Register the collection view cell
myCollectionView.register(UINib(nibName: "MyCell", bundle: nil), forCellWithReuseIdentifier: "MyCell")
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as? MyCell else {
fatalError("The dequeued cell is not an instance of MyCell.")
}
return cell
}
然而,无论我做什么,Xcode 都会崩溃并给我这个错误:
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使类型视图出列:带有标识符 MyCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或类或连接故事板中的原型单元格”
我试过清理构建文件夹,重新启动 Xcode,甚至重新启动我的 Mac,但没有任何效果。
我究竟做错了什么??
解决方案
我认为你必须检查两件事
使用这个 myCollectionView.dequeueReusableCell
` 覆盖 func awakeFromNib() { super.awakeFromNib()
myCollectionView.delegate = self myCollectionView.dataSource = self // Register the collection view cell myCollectionView.register(UINib(nibName: "MyCell", bundle: nil), forCellWithReuseIdentifier: "MyCell") } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as? MyCell else { fatalError("The dequeued cell is not an instance of MyCell.") } return cell }`
打开 Xib 文件并检查是否设置了可重用标识符
希望这能解决你的问题,如果不让我知道
推荐阅读
- scala - ScalaTest:仅用于选定测试的 setup/tearDown 方法
- python - Matplotlib 对条形图进行错误排序
- java - 需要在 WebDriver 中选中一个复选框
- python - 如何从 Repo 安装 jasmin SMS Gateway the systemd
- javascript - 如何在 Python 中使用 Selenium 来获取 JavaScript 元素?
- c++ - 无法通过测试(Kadane 算法)
- vue.js - 状态更改时更新 getter 值 Vuex 存储
- python - 为什么添加到 OrderedDict 并在函数内部排序的键不会在函数外部保持排序?
- bash - 在 bash 中使用“exit ${?}”是否正确?
- spring-boot - Spring Retry 不再调用 maxAttempts