ios - Swift - 无法使 : UICollectionElementKindCell 类型的 View 出队

标签 ios swift cocoapods jtapplecalendar

我在尝试加载我的项目时收到此错误消息

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使类型的 View 出列:具有标识符 CustomCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者连接 Storyboard中的原型(prototype)单元格”

我的代码是:

extension ViewController: JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = Calendar.current.timeZone
        formatter.locale = Calendar.current.locale

        let startDate = formatter.date(from: "2017 01 01")!
        let endDate = formatter.date(from: "2017 12 31")!

        let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
        return parameters
    }

    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.dateLabel.text = cellState.text
        return cell
    }
}

请帮我调试这个问题。谢谢。

编辑:我已经为单元格添加了标识符,但错误仍然存​​在。

Storyboard

最佳答案

viewDidLoad 方法中: 您必须使用 collectionView 对象注册您的自定义单元类/xib 名称。

如果你只有类自定义类那么你通过以下方式注册(没有xib文件)

collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "cellId")

如果你既有class又有xib文件那么你可以通过这种方式注册

collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "cellId")

关于ios - Swift - 无法使 : UICollectionElementKindCell 类型的 View 出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43734614/

相关文章:

ios - 在类中初始化协议(protocol)

ios - 如何在 Flutter 中禁用 InAppWebView 的缩放?

swift - private 和 fileprivate 顶级类之间的区别

ios - -U 和 -bitcode_bundle(Xcode 设置 ENABLE_BITCODE=YES)不能在 Xcode 13 中一起使用

ios - 通过 Cocoapods 安装 RestKit 0.20.0 后基础类型未知

ios - 我怎样才能调用其他类按钮 - Swift

ios - 我如何在 alamofire post 请求中发送 JSON 变量作为参数

ios - 一旦安装到 Xcode 项目中就无法使用 cocoa pod 模块

ios - 将 SKShapeNode 放置在彼此之上以获得图形效果

ios - 如何使 UILabel 在 UIView 上居中