异常:'NSInternalInconsistencyException',原因:'无法在捆绑包中加载 NIB:'NSBundle..... 在 Swift 4

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

这是我的视图控制器,我在其中设置了我的 collectionView 及其方法。

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
}

这是我的自定义单元格文件

class CustomCellOneCollectionViewCell: UICollectionViewCell {

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

}

我还使用了下面的代码来声明单元格。

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

我检查了所有连接,仔细检查了标识符的名称。 我的视图控制器是选项卡栏控制器的一部分。

确保您的单元格 recommendedcell1 确保它的超类是 CustomCellOneCollectionViewCell

的类型

为什么这条线

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

dequeueReusableCell

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

因此您的手机将是

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

    let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! CustomCellOneCollectionViewCell

    return cell
}

删除此行

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

试试这个

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

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

}

注册nib时,nibName应该是Cell namee意思是CustomCellOneCollectionViewCell

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

并且在 cellForItemAt

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