I have bug that I can't find solution, so I need your help.
I have class MealsViewController, subclass UIViewController with Table view in my ViewController. Then I create class MealTableViewCell with custom cell xib file for image and label.
Everything between ViewController and MealsViewController class is connected, and I called the correct identifier. Also, everything between custom xib file and MealTableViewCell is connected.
When I run my code on device I have the following error message:
2023-01-12 13:07:49.258961 0100 MealsAndDrinksApp[4137:905198] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: ****'Could not load NIB in bundle: 'NSBundle** **</private/var/containers/Bundle/Application/131F07AD-8782-4711-B2D6-2D778D57EEE4/MealsAndDrinksApp.app> (loaded)' with name 'MealTableVIewCell''
*** First throw call stack:
(0x1b6cc7a84 0x1b5119958 0x1b6dac36c 0x1bdea5688 0x1bddfbf38 0x1bddfbcc4 0x1bdccef9c 0x1bdcceea0 0x102c2878c 0x102c28a78 0x1bdd6a454 0x1bdd40030 0x1bdc826e4 0x1bdc821c8 0x1bdc31ff8 0x1bd151230 0x1bd163c9c 0x1bd1748a0 0x1bd1a90a8 0x1bd1911e8 0x1b6d497d0 0x1b6cd7894 0x1b6d350a0 0x1b6d39b7c 0x1f10c1984 0x1bdfa33c8 0x1bdfa3040 0x1c3e83240 0x102c0ed38 0x102c0ecc0 0x102c0edec 0x1d8750df0)
libc abi: terminating with uncaught exception of type NSException
terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/containers/Bundle/Application/131F07AD-8782-4711-B2D6-2D778D57EEE4/MealsAndDrinksApp.app> (loaded)' with name 'MealTableVIewCell''
(lldb)
And an error in my AppDelegate: screenshot of IDE with error highlighted
Thread 1: "Could not load NIB in bundle: 'NSBundle </private/var/containers/Bundle/Application/131F07AD-8782-4711-B2D6-2D778D57EEE4/MealsAndDrinksApp.app> (loaded)' with name 'MealTableVIewCell'"
Thanks in advance for help.
I expect to see data from url in tableView image and label, but I do not see tableview when I star the project. Just an error message.
I uploaded the project to GitHub. The link is below: https://github.com/zmnbgd/Meals-Drinks-/tree/main But I will post a minimal repeatable example here.
//MARK: Meal Model
struct MealsModes: Codable {
var meals: [Meals]
}
struct Meals: Codable {
var strMeal: String?
var strArea: String?
var strMealThumb: String?
var strSource: String?
var strYoutube: String?
}
//MARK: MealsViewController
extension MealsViewController {
private func setUpMeals() {
setUpDMealsDelegate()
setUpTableViewCollection()
}
private func setUpDMealsDelegate() {
meals.delegate = self
meals.dataSource = self
}
private func setUpTableViewCollection() {
let nib = UINib(nibName: MealTableViewCell.mealCellIdentifier, bundle: nil)
meals.register(nib, forCellReuseIdentifier: MealTableViewCell.mealCellIdentifier)
}
}
//MARK: Presenting Meals Data in MealsViewController TableView
extension MealsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return meals1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MealTableViewCell.mealCellIdentifier, for: indexPath) as? MealTableViewCell else {
return UITableViewCell()
}
let item = meals1[indexPath.row]
cell.setUpMealCell(data: item)
return cell
}
}
//MARK: MealTableViewCell Swift File
class MealTableViewCell: UITableViewCell {
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var mealAreaLabel: UILabel!
@IBOutlet weak var sourceLabel: UILabel!
@IBOutlet weak var mealImage: UIImageView!
//MARK: Meal Cell Identifier
static let mealCellIdentifier = "MealTableVIewCell"
override func awakeFromNib() {
super.awakeFromNib()
}
//MARK: SetUp Cell Image And Labels
func setUpMealCell(data: Meals) {
mealNameLabel.text = data.strMeal
mealAreaLabel.text = data.strArea
let mealImageUrl = URL(string: data.strMealThumb ?? "")
mealImage.kf.setImage(with: mealImageUrl)
}
}
CodePudding user response:
SOLUTION:
I had a problem when I start a project on the simulator or the device, I can't see the TableView in the ViewController, or the custom cell - xib file. I had an error message: Could not load NIB in bundle: 'NSBundle when I run the project.
The code was without error and everything was well connected between the ViewController, TableView and CustomTableViewCell.
I fixed the bug by deleting the MealTableViewCell and MealTableViewCell - xib file and creating them again.