为什么我的集合视图单元格返回错误“出队单元格不是TileCollectionViewCell的实例”

时间:2019-05-07 22:27:14

标签: swift uicollectionview

我正在通过情节提要为我的应用程序构建一个集合视图,并且我已经完成了该集合视图的整个设置。我创建了UICollectionViewController,并且为UICollectionViewController创建了一个代码文件。我还为原型CollectionViewCell创建了一个代码文件,并为其分配了重用标识符。我创建了一条让所有错误都可以检测到的致命警戒语句,并且已经将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语句中将and错误返回到控制台:

  

出队的单元格不是TileCollectionViewCell的实例

为什么返回此错误?

2 个答案:

答案 0 :(得分:1)

设置集合视图时调用register(_:forCellWithReuseIdentifier:)方法

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

答案 1 :(得分:0)

要检查的事情:

  1. 检查在情节提要中分配的可重复使用的标识符是否与“ TileCell”匹配。
  2. 收集视图中的检查单元格已分配到情节提要中的TileCollectionViewCell类。
相关问题