gpt4 book ai didi

ios - 从 xib 实例化自定义表格 View 单元格失败

转载 作者:行者123 更新时间:2023-11-28 06:27:50 25 4
gpt4 key购买 nike

这是我的表格 View 单元格的继承树。

 +----------------+
| UITableViewCel |
+-------+--------+
^
|
+-------+--------+
| BaseFormCell |
+-------+--------+
^
|
+--------+---------+
| TypedFormCell<T> |
+--------+---------+
^
|
+--------+----------+
| TextFieldFormCell | : TypedFormCell<String>
+-------------------+

其中 TextFieldFormCell 有一个关联的 xib 文件,用于实例化自身。

当我调用 Bundle.main.loadNibNamed("TextFieldFormCell", owner: nil, options: nil) 时,会抛出异常并显示

[< UITableViewCell 0x7fe89584fa00 > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameLabel.

我注意到 xib 没有为我实例化 TextFieldFormCell。相反,它创建了一个 UITableViewCell 并尝试将 nameLabel 注入(inject) UITableViewCell,这导致了异常。

这是否意味着 IB 不支持泛型类或从泛型类继承的类?

这是该演示的 GitHub 存储库。 https://github.com/hikui/TypedCellDemo

最佳答案

另一种替代方式:

在你的类viewDidLoad中注册nib文件

    let nibName = UINib(nibName: "<nibName>", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "<CellIdentifier>")

在您的cellForRowAtIndexPath中:

    let cell : TextFieldFormCell? = tableView.dequeueReusableCell(withIdentifier: "<CellIdentifier>") as! TextFieldFormCell?

关于ios - 从 xib 实例化自定义表格 View 单元格失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com