ios - 尝试找到的每个建议后,IBOutlet 仍然为零
问题描述
我已经阅读了关于 SO 的“所有”步骤,并且无法修复我在 TableViewCell.xib 中的 IBOutlets 仍然为零并且从未调用过 awakeFromNib() 的错误。
错误
productTitle.text = item?.product.description
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
//item?.product.description is not nil
我试过的:
- 清理构建文件夹
- 删除派生数据文件夹
- 从头开始创建了一个全新的 Xib 和 .swift 类(在错误首次出现之后)
_ = self.view
在 CartViewController 中添加viewWillAppear
- 尝试将新标签添加到其他 XIB 以检查是否发生错误 - 它没有
- 在 XIB 中,验证了:
- 插座已连接(多次执行此操作)
- 自定义类 = CartProductCell
- 文件所有者 = CartProductCell
- 重用标识符 = CartProductCell
- 在 Storyboard 中,验证了:
- 单元格类 = CartProductCell
- 重用标识符 = CartProductCell
我的手机:
class CartProductCell: UITableViewCell {
@IBOutlet weak var productTitle: UILabel!
override func awakeFromNib() {
print("awaken")
}
var item: orderItem? {
didSet {
productTitle.text = item?.product.description
}
}
}
出队:
class CartViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = sections[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "CartProductCell", for: indexPath ) as! CartProductCell
cell.item = item
return cell
}
}
实例化 ViewController
extension UIViewController {
@objc func viewCart() {
if Cart.sharedInstance.order.items.count == 0 {
if let cartVC = storyboard?.instantiateViewController(withIdentifier: "EmptyCart") {
let cartNavigationController = UINavigationController(rootViewController: cartVC)
self.present(cartNavigationController, animated: true)
}
} else {
if let cartVC = storyboard?.instantiateViewController(withIdentifier: "Cart") as? CartViewController {
let cartNavigationController = UINavigationController(rootViewController: cartVC)
self.present(cartNavigationController, animated: true)
}
}
}
}
更新
- XIB 用于 UTTableViewCell,它是由故事板标识符注册的
- 插座是通过从标签拖动到代码来连接的,而不是在文件的所有者中
有什么线索吗?
解决方案
@Larme 在评论中提供了这里的答案。
问题是我在故事板 tableViewController 和 cell.XIB 中有一个具有相同标识符的原型单元。因此它使用了故事板中的单元格,而不是我的代码所在的 XIB。
您没有使用来自 xib 的单元格的界面版本,而是来自情节提要。因此它没有出口链接
推荐阅读
- python - keras中深度和广泛模型的问题
- java - 图形 2d 平移和旋转问题
- ruby - 将比较运算符作为参数传递给 Ruby 中的方法
- mongodb - 如何将 db mongo 复制到另一个 mongodb 实例?
- c++ - 三角带上的纹理坐标
- r - RShiny - 从列表中渲染信息框
- android - requestPermissions 不适用于 READ_CALL_LOG 和 WRITE_CALL_LOG 在 android 8.1 API 27
- javascript - 如何将实例变量从 React 组件传递到其 HOC?
- java - 通过 Jackson JSON 将 Java 日期解析为 C#
- c++ - 分段错误,由错误的区间(二维数组)引起