首页 > 解决方案 > 自定义单元格导致“NSInternalInconsistencyException”,原因:“无法在包中加载 NIB:

问题描述

我正在尝试使用 XIB 文件将自定义单元格添加到我的表中,当我尝试运行它时出现此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'parentCell''

这些是我为到达这里所采取的步骤:

1) 创建自定义 xib 故事板单元格 2) 将故事板单元格的标识符设置为 parentCell 3) 为自定义 xib 创建一个类为 ParentTableViewCell 4) 将 xib 故事板单元格的类设置为 ParentTableViewCell 5) 在主情节提要,并在主 ViewController 中为名为 itemsTable 的表制作了一个 IBOutlet。6)在主ViewController的viewdidload中设置:

itemsTable.dataSource = self
itemsTable.delegate = self
itemsTable.register(UINib(nibName: "parentCell", bundle: nil), forCellReuseIdentifier: "parentCell")

7) 在 cellForRowAt 中:

let cell = tableView.dequeueReusableCell(withIdentifier: "parentCell", for: indexPath) as! ParentTableViewCell
return cell

你们能看看我是否错过了导致问题的步骤吗?

我试过 command + shift + k 并显示相同的错误。

标签: swiftxcodeuitableviewnibcustom-cell

解决方案


如果 xib 名称是ParentTableViewCell.xib那么你应该做

itemsTable.register(UINib(nibName: "ParentTableViewCell", bundle: nil), forCellReuseIdentifier: "parentCell")

推荐阅读