Swift TableViewCell,无法加载我的自定义 TableViewCell

时间:2021-07-21 00:05:24

标签: swift uitableview

我正在创建一个非常简单的应用程序,当我尝试使 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")

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我重做了整个 xib 文件,它奏效了!不确定确切的问题是什么!

相关问题