簡體   English   中英

Swift - 無法使同類視圖出隊:UICollectionElementKindCell

[英]Swift - could not dequeue a view of kind: UICollectionElementKindCell

我在嘗試加載我的項目時收到此錯誤消息

由於未捕獲的異常“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
    }
}

請幫我調試這個問題。 謝謝你。

編輯:我已經添加了單元格的標識符,但仍然存在錯誤。

故事板

viewDidLoad方法中:您必須使用 collectionView 對象注冊自定義單元格類/xib 名稱。

如果你只有類自定義類,那么你可以通過以下方式注冊(沒有 xib 文件)

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

如果你同時擁有 class 和 xib 文件,那么你可以通過這種方式注冊

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

這是因為您尚未將 xib 注冊到UICollectionView 如果您使用UICollectionViewCell的 UICollectionViewCell,您必須先注冊它。

In viewDidLoad: 

寫下來:

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

從您粘貼的代碼片段中,我沒有看到您在要出隊/使用它之前注冊該單元格。 由於描述的錯誤,需要在使用前注冊單元格。

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

為了您的快速參考,請參閱下面的帖子 - https://www.hackingwithswift.com/example-code/uikit/how-to-register-a-cell-for-uitableviewcell-reuse


截圖發布后編輯(如果適合你,請接受這個答案^_^)

從屏幕截圖中,您正在使用自定義單元格類“CustomCell”,在設置正確的標識符后,請確保在右側面板的身份檢查器中將正確的類名設置為 CustomCell。 請參閱下面的屏幕截圖。 在此處輸入圖像描述

  1. 如果您在情節提要中使用單元格而不是XIB ,那么您需要設置 reuseIDentifier 如下在此處輸入圖像描述

  2. 如果您對單元格使用單獨的XIB ,則需要在ViewDidLoad中添加此行

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

這個問題有很多可能性。 如果您有collection view的自定義類,那么它應該在identity inspector中設置。 而且它必須是UICollectionViewCell的子類。 並且您必須使用必須在attribute inspector器中設置的適當reusable identifier在您的cellforitem委托方法中實例化該子類。 在您的屏幕截圖中, Apple calender view似乎是您的收藏視圖。 如果它是第三方庫,那么請確保它允許您對自定義單元格進行雙端隊列! 並確保它是UICollectionView的子類。

在我的案例中,我創建了一個帶有類的自定義單元格的集合視圖
CollectionView + 自定義集合單元格 + 自定義集合單元格類 重用標識符 自定義集合視圖類

這是我的自定義 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)
  }      
}

玩得開心,祝你好運。 隨意編輯我的答案

我和我有完全相同的問題。 環顧四周后,我意識到我有一個小寫字母“p”,而它應該是一個大寫字母“P”,我使用的名稱作為標識符。

單擊故事板上的 collectionView 單元格,打開右側面板。 檢查屬性檢查器和集合可重用視圖、標識符名稱,還要檢查身份檢查器、自定義類名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM