主故事板中XIB自定义UITableView单元格出队失败求助
在主故事板中使用XIB自定义UITableViewCell时触发NSInternalInconsistencyException异常
在主故事板的UITableView中尝试出队使用XIB设计的自定义单元格时,抛出NSInternalInconsistencyException异常,错误提示:
unable to dequeue a cell with identifier cellCustom - must register a nib or a class for the identifier or connect a prototype cell in a storyboard.
已尝试的排查步骤
- 确认单元格注册时的复用标识符和出队时完全一致
- 移除并重新添加单元格文件到项目编译目标
- 在其他故事板中测试相同代码,功能正常,仅主故事板出现问题
- 在主故事板中使用原型单元格时,功能完全正常
- 检查XIB文件中单元格的自定义类与代码中的类名匹配
- 确认XIB中的插座连接到单元格视图而非文件所有者
- 执行清理构建并清除派生数据
相关代码
视图控制器中的单元格注册代码
override func viewDidLoad() { super.viewDidLoad() let nib = UINib(nibName: "CustomCell", bundle: nil) tableView.register(nib, forCellReuseIdentifier: "cellCustom") let anotherNib = UINib(nibName: "AnotherCell", bundle: nil) tableView.register(anotherNib, forCellReuseIdentifier: "cellAnother") }
单元格出队代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 { guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellCustom", for: indexPath) as? CustomCell else { fatalError("Unable to dequeue cell with identifier: cellCustom") } return cell } else { guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellAnother", for: indexPath) as? AnotherCell else { fatalError("Unable to dequeue cell with identifier: cellAnother") } return cell } }
完整错误日志
2024-07-27 22:18:23.234513+0530 test[4203:4352355] *** Assertion failure in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], UITableView.m:9818 2024-07-27 22:18:23.242276+0530 test[4203:4352355] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cellCustom - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' *** First throw call stack: (0x1805afc60 0x197ddfee4 0x181d54c80 0x182da33bc 0x182a4263c 0x10481e364 0x104824598 0x182ac7f8c 0x182cb284c 0x182aebea8 0x1829c4428 0x1829d4174 0x184054520 0x184047294 0x18405acc8 0x18406379c 0x1829ae708 0x18055b76c 0x18052c974 0x180527efc 0x18053b174 0x1a107a988 0x182d3da88 0x182ad6f78 0x196ec7ee4 0x104be2bd0 0x104be2b48 0x104bfe3dc 0x10680c4d0) libc++abi: terminating with uncaught exception of type NSException
解决方案
清理主故事板的标识符冲突
主故事板中如果曾存在过cellCustom标识符的原型单元格,Xcode可能残留缓存导致冲突。打开主故事板全局搜索cellCustom,确认没有残留的原型单元格;再次执行Cmd+Shift+K清理构建文件夹,并删除Xcode的派生数据(路径:Xcode -> 设置 -> 位置 -> 派生数据 -> 删除)。确保注册代码被执行
在viewDidLoad的注册代码处加断点,确认代码确实被调用。如果视图控制器是通过segue延迟初始化,可能出现单元格未注册就开始出队的情况,可将注册代码移至init?(coder: NSCoder)或viewWillAppear(_:)中尝试。验证XIB的编译目标
选中CustomCell.xib和AnotherCell.xib,查看右侧检查器的「Target Membership」,确保勾选了当前项目的App Target,避免XIB未被编译进项目。指定精确的Bundle注册XIB
替换nil为自定义单元格类对应的Bundle,避免路径解析问题:let nib = UINib(nibName: "CustomCell", bundle: Bundle(for: CustomCell.self)) tableView.register(nib, forCellReuseIdentifier: "cellCustom")重置主故事板的TableView配置
删除主故事板中TableView的数据源、代理连接后重新关联;或直接删除现有TableView,拖入新的TableView重新配置数据源和代理,排除故事板文件损坏的可能。
内容的提问来源于stack exchange,提问作者bumblebee
相关产品推荐
相关产品推荐

