1

This is my View Controller, in which I have set my collectionView and with its methods.

class HomeScreenViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { @IBOutlet weak var collectionViewOne: UICollectionView! override func viewDidLoad() { super.viewDidLoad() collectionViewOne.register(UINib(nibName: "recomendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1") // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell return cell } 

this is my custom cell file

class CustomCellOneCollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! override func awakeFromNib() { super.awakeFromNib() // Initialization code } 

}

I have also used code below, for cell declaration.

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell 

I have checked all connections, double-checked identifiers' names. my view controller is a part of tab-bar controller.

5 Answers 5

3

While registert nib, nibName shoudbe Cell namee means CustomCellOneCollectionViewCell

collectionViewOne.register(UINib(nibName: "CustomCellOneCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell1") collectionViewOne.delegate = self collectionViewOne.datasource = self 

and in cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell return cell } 
Sign up to request clarification or add additional context in comments.

Comments

0
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! CustomCellOneCollectionViewCell return cell } 

Comments

0

remove this line

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell return cell } 

try this one

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "recommendedcell1", for: indexPath) as! CustomCellOneCollectionViewCell return cell } 

1 Comment

getting the same exception!
0

Make sure that your cell recommendedcell1 Make sure that its SuperClass is type of CustomCellOneCollectionViewCell

enter image description here

Why this line

let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell 

just dequeueReusableCell

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell 

So your cell will be

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell return cell } 

2 Comments

Because I was getting the same exception with that one too. Thanks I will edit my question.
still ain't working, I have made sure its superclass is the same you mentioned.
0

For me the problem was about File Target.

Make sure your xib file added to all targets.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.