Reputation: 103
I got this error message when trying to load my project
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CustomCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
My code is:
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
}
}
Please help me debug this problem. Thank you.
Edit: I have added the identifier for the cell, but still the error exists.
Upvotes: 3
Views: 23650
Reputation: 121
From the code snippets you pasted, I don't see you register the cell before you want to dequeue/use it. Need to register the cell before use as the error described.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
For your quick reference, please see below post - https://www.hackingwithswift.com/example-code/uikit/how-to-register-a-cell-for-uitableviewcell-reuse
Edit after screenshot posted
From the screenshot, you are using a custom cell class 'CustomCell', after you set the correct identifier, please make sure setup the correct class name as CustomCell in the identity inspector of right panel. Refer to below screenshot.
Upvotes: 4
Reputation: 3647
It is because you haven't registered your xib to UICollectionView
. If you are using UICollectionViewCell
from Xib, you must register it first.
In viewDidLoad:
write it:
if let xib = NSNib.init(nibNamed: "TemplateNBgCollectionItem", bundle: nil) {
self.collectionView.register(xib, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cvItem"))
}
Upvotes: 7
Reputation: 31
I had the exact same issue with mine. and after looking around i realised i had a lower case "p" when it should've been an upper case "P" with the name i used for an identifier.
click on the collectionView cell on the storyboard, open the right panel. Check attributes inspector and collection Reusable View, identifier name, but also check the identity inspector, custom class name.
Upvotes: 0
Reputation: 1076
In viewDidLoad
method:
You must register your custom cell class/xib name with collectionView object.
If you have only class custom class then you register by following way (No xib file)
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "cellId")
If you have both class as well as xib file then you can register by this way
collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "cellId")
Upvotes: 10
Reputation: 1808
In My Case
Im creating a collection View with custom cell with class
This Is my Custom CollectionView Class Code
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
}
}
Connect all the view from your storyboard to Custom Class by CTRL+Drag and @IBOutlet will be created.
I Called my custom class like this
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)
}
}
Have fun And Good Luck. Feel Free to edit my answer
Upvotes: 0
Reputation: 27428
There are many possibilities for this issue. If you have custom class for collection view
then it should be set in identity inspector
. And it must be subclass of UICollectionViewCell
. And you have to instantiate that subclass in your cellforitem
delegate method with proper reusable identifier
that must be set in attribute inspector
. Here in your screen shot, It seems like Apple calender view
is your collection view. If it is third party library then make sure that it is allowing you to deque your custom cells! and make sure it is subclass of UICollectionView
.
Upvotes: 0
Reputation: 8322
If you use cell in storyboard not XIB then you need to set reuseIDentifier as below
If you use separate XIB for cell then you need to add this line in ViewDidLoad
collectionView.register(UINib(nibName: "NibName", bundle: nil), forCellWithReuseIdentifier: "reuseIdentifier")
Upvotes: 3