【问题标题】:Swift - could not dequeue a view of kind: UICollectionElementKindCell斯威夫特 - 无法出列同类视图:UICollectionElementKindCell
【发布时间】:2017-05-02 09:37:41
【问题描述】:

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

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 CustomCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元格”

我的代码是:

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
    }
}

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

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

【问题讨论】:

标签: ios swift cocoapods jtapplecalendar


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 我用过这个 但是我仍然有问题 let cell = calendar.dequeueReusableJTAppleDayCellView(withReuseIdentifier: "JTCustomCell", for: indexPath) as! JTCustomCell
【解决方案2】:

这是因为你还没有将你的xib注册到UICollectionView。如果您使用的是 Xib 的UICollectionViewCell,则必须先注册。

In viewDidLoad: 

写下来:

if let xib = NSNib.init(nibNamed: "TemplateNBgCollectionItem", bundle: nil) {
   self.collectionView.register(xib, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cvItem"))  
}

【讨论】:

  • 对不起,我对此很陌生。当我尝试您的建议时,我收到以下错误 - Use of unresolved identifier 'collectionView'
  • 哦,那是因为我将 collectionView 作为虚拟文本。你要做的是:创建一个名为 collectionView 的 UICollectionView 的 IBOutlet。那你就可以走了
  • 你能告诉我我到底需要做什么以及我需要把它放在哪里。抱歉,这是我第一次这样做
  • 转到 IB 上的那个控制器。 Ctrl + 拖动到你的这个控制器类。然后当它提示时,使用名称:collectionView
【解决方案3】:

从您粘贴的代码 sn-ps 中,我没有看到您在要出列/使用它之前注册单元格。如所描述的错误,需要在使用前注册单元格。

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")

为了您的快速参考,请参阅下面的帖子 - https://www.hackingwithswift.com/example-code/uikit/how-to-register-a-cell-for-uitableviewcell-reuse


截图发布后编辑(如果适合你,请接受这个答案^_^)

从屏幕截图中,您使用的是自定义单元格类“CustomCell”,设置正确的标识符后,请确保在右侧面板的身份检查器中将正确的类名设置为 CustomCell。请参阅下面的屏幕截图。

【讨论】:

  • 我看到你更新后的截图,连接IBOutlet时要小心,并确保名称与你的xib匹配。或者您可以重新连接以仔细检查。祝你好运
【解决方案4】:
  1. 如果您在情节提要中使用单元格而不是XIB,则需要将reuseIDentifier 设置为如下

  2. 如果你对单元格使用单独的XIB,那么你需要在ViewDidLoad中添加这一行

    collectionView.register(UINib(nibName: "NibName", bundle: nil), forCellWithReuseIdentifier: "reuseIdentifier")

【讨论】:

    【解决方案5】:

    这个问题有很多可能性。如果您有collection view 的自定义类,那么它应该在identity inspector 中设置。它必须是UICollectionViewCell 的子类。你必须在你的cellforitem委托方法中实例化那个子类,并且必须在attribute inspector中设置正确的reusable identifier。在您的屏幕截图中,Apple calender view 似乎是您的收藏视图。如果它是第三方库,那么请确保它允许您将自定义单元格从队列中取出!并确保它是UICollectionView 的子类。

    【讨论】:

      【解决方案6】:

      就我而言 我正在创建一个带有类
      的自定义单元格的集合视图

      这是我的自定义 CollectionView 类代码

      import UIKit
      class Auction_CollectionCell: UICollectionViewCell {
      
      @IBOutlet weak var Productimage: UIImageView!
      @IBOutlet weak var name: UILabel!
      @IBOutlet weak var bid: UILabel!
      @IBOutlet weak var progress: UIProgressView!
      @IBOutlet weak var day: UILabel!
      @IBOutlet weak var hours: UILabel!
      @IBOutlet weak var mins: UILabel!
      @IBOutlet weak var secs: UILabel!
      @IBOutlet weak var lblday: UILabel!
      @IBOutlet weak var lblhours: UILabel!
      @IBOutlet weak var lblmin: UILabel!
      @IBOutlet weak var lblsecs: UILabel!
      @IBOutlet weak var MainView: UIView!
      
         override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
          }
      }
      

      通过 CTRL+Drag 将故事板中的所有视图连接到自定义类,@IBOutlet 将被创建。
      我这样调用了我的自定义类

      self.AuctionList = NSArray with NSDictionary Content

      func collectionView(_ collectionView: UICollectionView,
                          numberOfItemsInSection section: Int) -> Int {
          return self.AuctionList.count
      }
      func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
       let cell : Auction_CollectionCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? Auction_CollectionCell)!
      
          cell.lblday.text = "Day"
          cell.lblhours.text = "Hours"
          cell.lblmin.text = "Minutes"
          cell.lblsecs.text = "Secs"
      
          cell.MainView.layer.cornerRadius = 5
          cell.MainView.backgroundColor = UIColor.white
          cell.MainView.layer.borderWidth = 0.5
          cell.MainView.layer.borderColor = UIColor.gray.cgColor
      
      
          return cell
      }
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       var width = CGFloat(0)
       var height = CGFloat(0)
       if (UIDevice.current.userInterfaceIdiom == .pad){
         width = CGFloat((self.view.bounds.size.width / 4.0) - 15)
         height = CGFloat(246.0)
       }else{
         width = CGFloat((self.view.bounds.size.width / 2.0) - 15)
         height = CGFloat(246.0)
      }
      
         print("Resize Image \(width) \(height)")
         return CGSize(width: width, height: height)
      }
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      if let dic = AuctionList[indexPath.row] as? NSDictionary{
          Product.product_id = "\((dic["product_id"])!)"
          Product.product_image = "\((dic["image"])!)"
          Product.auction_id = "\((dic["auction_id"])!)"
          performSegue(withIdentifier: "auctionProductDetails", sender: self)
        }      
      }
      

      玩得开心,祝你好运。 随意编辑我的答案

      【讨论】:

        【解决方案7】:

        我的问题完全相同。环顾四周后,我意识到我有一个小写的“p”,而它本应该是一个大写的“P”,而我用作标识符的名称。

        点击故事板上的collectionView单元格,打开右侧面板。检查属性检查器和集合 Reusable View、标识符名称,还要检查身份检查器、自定义类名。

        【讨论】:

          猜你喜欢
          • 2015-10-13
          • 1970-01-01
          • 2017-07-22
          • 2017-03-24
          • 2020-05-27
          • 2018-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多