iOS开发求助:自定义TableViewCell的NIB文件加载失败
iOS 自定义TableViewCell XIB加载崩溃问题
问题描述
创建了继承自UIViewController的MealsViewController(内含TableView),以及自定义MealTableViewCell类和对应的XIB文件用于展示图片和标签。所有关联配置、标识符调用均已确认正确,但运行App时直接崩溃,抛出以下错误:
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)
App启动后无法显示TableView,直接崩溃,期望TableView能展示网络加载的图片和标签数据。
最小复现代码
数据模型
// 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? }
MealsViewController配置
// 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) } }
TableView代理实现
// 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 } }
MealTableViewCell实现
// 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) } }
问题分析与解决方案
核心问题:拼写错误
错误日志明确指出找不到名为MealTableVIewCell的NIB文件,而代码中mealCellIdentifier的值正是这个字符串——注意这里的VI是大写的V和I,而正确的TableView拼写应该是TableView(V后接小写的i)。这意味着实际的XIB文件名应为MealTableViewCell,但代码里用了错误的拼写去加载,导致找不到文件崩溃。
解决步骤
- 修正标识符拼写:将
MealTableViewCell中的标识符改为正确的拼写:static let mealCellIdentifier = "MealTableViewCell" - 验证XIB文件名:确认自定义XIB文件的文件名确实是
MealTableViewCell.xib,大小写完全匹配。 - 检查Bundle资源:在Xcode的
Build Phases->Copy Bundle Resources中,确保MealTableViewCell.xib已被添加,会被打包进App bundle。 - 确认XIB关联:打开XIB文件,检查Cell的Class是否设置为
MealTableViewCell,避免类名关联错误。
内容的提问来源于stack exchange,提问作者zmnbgd
相关产品推荐
相关产品推荐

