【问题标题】:Swift TableViewCell, unable to load my custom TableViewCellSwift TableViewCell,无法加载我的自定义 TableViewCell
【发布时间】:2021-07-21 00:05:24
【问题描述】:

我正在创建一个非常简单的应用程序,当我尝试将我的 tableview 单元格出列时出现此错误。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AnimalCell", for: indexPath) as! AnimalTableViewCell

    let animal = animals[indexPath.row]
    let thisLocation = locations.randomElement()
    cell.fillInTheDetails(animal: animal, location: thisLocation!)

    return cell
  }

当我调用它时遇到错误

 let cell = tableView.dequeueReusableCell(withIdentifier: "AnimalCell", for: indexPath) as! AnimalTableViewCell

报错信息如下:

TableViewWithMap[27537:9935669] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x600001224030> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key animalName.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff20422c5b -[NSException init] + 0
    3   Foundation                          0x00007fff207af46c

我已经在 viewDidLoad 函数中注册了这个 TableViewcell

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UINib(nibName: "AnimalTableViewCell", bundle: nil), forCellReuseIdentifier: "AnimalCell")

【问题讨论】:

  • 什么是“动物名”?

标签: swift uitableview


【解决方案1】:

您已使用 Interface Builder 在 XIB 中删除了一个 UIElement,该 UIElement 在您的视图控制器中有一个插座,而没有断开 IBOutlet 本身的连接。在您的 VC 中断开 animalName IBOutlet 的链接。

【讨论】:

  • 这个错误肯定是关于断开的 IBOutlet 连接。您将需要提供更多信息 - VC 的核心,以便我们可以看到插座发生了什么,以及您在自定义 AnimalTableviewCell 类中的任何代码。
【解决方案2】:

确保outlet 正确连接到 xib 文件。
并检查单元格的class nameidentifier是否匹配正确。

我查找了与您遇到相同错误的链接。
最好参考链接的答案。

【讨论】:

    【解决方案3】:

    我重新编写了整个 xib 文件,它成功了!不知道具体是什么问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      相关资源
      最近更新 更多