Swift 4中NSInternalInconsistencyException异常:无法加载NIB求助
Hey there, let's work through this NIB loading error you're hitting—it's super common, and we can track it down step by step!
First, let's look at the most obvious (and easy-to-miss) issue in your code:
1. Typos in NIB Names (The Likely Culprit!)
Looking at your viewDidLoad code, you're registering the NIB with this name:
collectionViewOne.register(UINib(nibName: "recomendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1")
But in cellForItemAt, you're trying to load a NIB named:
let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
See the difference? "recomendedcell1" vs "recommendedcell1"—you're missing an extra 'm' in the registered name. That's why the system can't find the NIB you're trying to load!
2. Stick to Consistent Cell Reuse Practices
You tried two approaches to get the cell: direct NIB loading and dequeuing. It's best to pick one and stick with it for consistency. The recommended method is registering the NIB once, then dequeuing cells:
Corrected Code:
override func viewDidLoad() { super.viewDidLoad() // Fix the NIB name to match your actual XIB file collectionViewOne.register(UINib(nibName: "recommendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1") } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // Use dequeue instead of direct NIB loading let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell // Configure your cell here (e.g., set imageView's image) return cell }
3. Verify Your XIB File Configuration
Even with the name fixed, double-check these XIB settings to rule out other issues:
- Target Membership: Open your
recommendedcell1.xibfile, select the file in the Project Navigator, and make sure your app's target is checked in the File Inspector (right panel). If it's not, the NIB won't be included in your app bundle. - Cell Identity: In the XIB, select the collection view cell, go to the Identity Inspector (right panel):
- Ensure the Class is set to
CustomCellOneCollectionViewCell(not just the defaultUICollectionViewCell). - Ensure the Reuse Identifier matches exactly what you're using in code:
"cell1".
- Ensure the Class is set to
- IBOutlet Connections: Confirm that the
imageViewin your custom cell is properly connected to the UIImageView in the XIB—broken connections can sometimes cause unexpected NIB loading errors too.
4. Clean and Rebuild Your Project
Sometimes cached build files can cause weird issues like this. Try:
- Press
Cmd + Shift + Kto clean the project. - Press
Cmd + Bto rebuild it from scratch. - Run your app again to see if the error is gone.
5. Tab Bar Controller Context
Your view controller being part of a tab bar controller shouldn't affect NIB loading, but just to be safe: confirm that your HomeScreenViewController is properly embedded in the tab bar controller, and that its collectionViewOne outlet is connected (you can add a print(collectionViewOne) in viewDidLoad to make sure it's not nil).
内容的提问来源于stack exchange,提问作者Parth Rudakia

