iosswiftxcodeswift4xcode9

Exception : 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle..... in Swift 4


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.


Solution

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