gpt4 book ai didi

swift - 为什么我的 Collection View 单元格返回错误 "The dequeued cell is not an instance of TileCollectionViewCell"

转载 作者:行者123 更新时间:2023-11-28 13:41:00 24 4
gpt4 key购买 nike

我正在通过 Storyboard为我的应用程序构建一个 Collection View ,并且我已经完成了 Collection View 的整个设置。我创建了 UICollectionViewController 并为 UICollectionViewController 创建了一个代码文件。我还为原型(prototype) CollectionViewCell 创建了一个代码文件,并为其分配了一个重用标识符。我创建了一个 guard-let 语句来检测任何 fatal error ,并且我已经将 CollectionViewCell 出队。

我将这段代码写在类行上方,导入语句下方。

private let reuseIdentifier = "TileCell"

我在类中编写了这个覆盖函数。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? TileCollectionViewCell else {
fatalError("The dequeued cell is not an instance of TileCollectionViewCell")
}

// Configure the cell

return cell
}

但是,当我在模拟器中运行该应用程序时,它崩溃并将 guard-let 语句中的错误信息返回到控制台:

The dequeued cell is not an instance of TileCollectionViewCell

为什么会返回这个错误?

最佳答案

设置 Collection View 时调用register(_:forCellWithReuseIdentifier:)方法

collectionView.register(TileCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

关于swift - 为什么我的 Collection View 单元格返回错误 "The dequeued cell is not an instance of TileCollectionViewCell",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031223/

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