这是我的 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/