ios - 异常 : 'NSInternalInconsistencyException' , 原因 : 'Could not load NIB in bundle: ' NSBundle. .... 在 Swift 4 中

标签 ios swift xcode swift4 xcode9

这是我的 View Controller ,我在其中设置了我的 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

我检查了所有连接,仔细检查了标识符的名称。 我的 View Controller 是标签栏 Controller 的一部分。

最佳答案

注册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
}

关于ios - 异常 : 'NSInternalInconsistencyException' , 原因 : 'Could not load NIB in bundle: ' NSBundle. .... 在 Swift 4 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507873/

相关文章:

ios - 在具有大标题的导航 Controller 中时,UIRefreshControl 未横向显示

ios - 像UItabbar一样完全透明和 float

ios - 使用自定义单元格类时出现 Tableview 错误

ios - 委托(delegate)按钮单击在 tableview 单元格中不起作用

ios - 将 UIImage 分配给 UIImageView.image 后崩溃

ios - 如何使用 Swift 从 Firebase 中的当前用户检索数据?

swift - Swift 4 中的过滤字典在 Xcode 中失败,但在 Playground 中成功

ios - Swift - 如何将 View Controller 的引用传递给子 UIView 类?

ios - 无法将类型 'UITableViewCellContentView' (0x111d61280) 的值转换为 'UIImageView' (0x111d4cdd0)

swift - 如果按钮宽度大于按钮图像的宽度(xcode),则将按钮图像保持在左侧位置