删除 xib 文件会导致 "NSInternalInconsistencyException, reason: 'Could not load NIB in bundle: "

Deleting xib files causes "NSInternalInconsistencyException, reason: 'Could not load NIB in bundle: "

当我为我的视图控制器删除 xib 文件时,我得到了这个错误:"Could not load NIB in bundle...with name 'LibraryViewController'"。我引用了这个 question.

我已经做过的事情:

有什么方法可以删除未使用的 xib 文件而不会使我的应用程序崩溃?

更新:

 override func viewDidLoad() {
      libraryTable.register(UINib(nibName: "categoriesTableViewCell", bundle: nil), forCellReuseIdentifier: "categoriesTableViewCell")
 }

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0  {
        let cell = tableView.dequeueReusableCell(withIdentifier: "categoriesTableViewCell", for: indexPath) as! categoriesTableViewCell
        return cell
    } else {
        let cell = songTableViewCell()
        return cell
    }
}

替换您注册单元格的代码(您现在希望在代码中创建而不是.xib/storyboard),如下所示:

libraryTable.register(UITableViewCell.self, forCellReuseIdentifier: "categoriesTableViewCell")

您在上面显示的行注册了 nib,因此当需要创建一个单元格时,您已经在某个 nib 文件中告诉它了。替换行以编程方式创建默认单元格,这意味着您需要使用 swift 代码而不是 InterfaceBuilder 提供的 GUI 方法对其进行任何修改。非常简单。