无法出列同类视图:带有标识符的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类
Posted
技术标签:
【中文标题】无法出列同类视图:带有标识符的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类【英文标题】:Could not dequeue a view of kind: UICollectionElementKindCell with identifier - must register a nib or a class for the identifier 【发布时间】:2020-05-25 22:17:21 【问题描述】:这让我发疯了。我过去整理了几十个集合视图,但这个拒绝工作。
我的收藏视图单元格有一个 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,但没有任何效果。
我做错了什么??
【问题讨论】:
请添加cellForItem
代码并检查UINib
是否正在加载
@Rikh 更新了问题
这段代码在 ViewController 中?
@King.lbt 在 UIView 自定义类中
【参考方案1】:
检查您是否提供了完全相同的标识符。 Copy/Paste
以确保它们相同。
【讨论】:
是的,完全一样。 xib 是否添加到您的项目目标中? 好像是,是的 尝试打印出UINib(nibName: "MyCell", bundle: nil)
看看你得到了什么?
@user93489112 awakeFromNib
方法被调用了吗?【参考方案2】:
我认为你必须检查两件事
使用这个 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文件,查看是否设置了可复用标识符
希望这能解决你的问题,如果没有,请告诉我
【讨论】:
以上是关于无法出列同类视图:带有标识符的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类的主要内容,如果未能解决你的问题,请参考以下文章
无法将 UICollectionView 的单元格所需的视图出列
错误无法使 UICollectionElementKindCell 类型的视图出列
无法使用标识符 Y (NSInternalInconsistencyException) 将类型 X 的视图出列
SWIFT:在具有多个部分的表视图中显示字典时,无法将具有标识符单元格错误的单元格出列
UIButton - NSInternalInconsistencyException - 无法将具有标识符 Cell2 的单元格出列