3

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.

Storyboard

Sid
  • 103
  • 1
  • 2
  • 12

7 Answers7

10

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")
Jacolack
  • 1,365
  • 2
  • 11
  • 25
Balaji Galave
  • 1,066
  • 6
  • 9
  • I used This But I still having problem with line let cell = calendar.dequeueReusableJTAppleDayCellView(withReuseIdentifier: "JTCustomCell", for: indexPath) as! JTCustomCell – Saeed Rahmatolahi Jun 05 '17 at 07:02
7

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"))  
}
Awais Mobeen
  • 733
  • 11
  • 19
ankit
  • 3,537
  • 1
  • 16
  • 32
  • I am sorry, but I am very new to this. When i tried your suggestion, I get the following error - Use of unresolved identifier 'collectionView' – Sid May 02 '17 at 09:49
  • oh thats because I have taken collectionView as dummy text. what you have to do is this: make an IBOutlet of UICollectionView named collectionView. Then you are good to go – ankit May 02 '17 at 09:50
  • Can you please tell me what exactly I need to do and where i need to put it. Sorry, but this is my first time doing all of this – Sid May 02 '17 at 10:01
  • Go to that controller on IB. Ctrl + drag to your this controller class. then when it prompts , use name : collectionView – ankit May 02 '17 at 10:03
4

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. enter image description here

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Aaron
  • 121
  • 5
  • I see your screenshot after you update, just be careful when you connect the IBOutlet, and make sure the name is matched with your xib. or you can re-connect it to double check. Good Luck – Aaron May 02 '17 at 10:05
3
  1. If you use cell in storyboard not XIB then you need to set reuseIDentifier as belowenter image description here

  2. 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")

KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

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.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

In My Case Im creating a collection View with custom cell with class
CollectionView + Custom Collection Cell + Custom Collection Cell Class Reused Identifier Custom Collection View 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

Muhammad Asyraf
  • 1,748
  • 15
  • 20
0

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.

wilkinsonncl
  • 31
  • 1
  • 5