从xib实例化自定义表视图单元格失败

时间:2016-12-26 09:22:01

标签: ios swift xcode

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

TextFieldFormCell

其中Bundle.main.loadNibNamed("TextFieldFormCell", owner: nil, options: nil)有一个关联的xib文件,用于实例化自己。

当我致电UITableViewCell时,会抛出异常并说

  

并[d UITableViewCell 0x7fe89584fa00> setValue:forUndefinedKey:]:this   class不是密钥nameLabel的密码值编码。

我注意到xib没有为我实例化TextFieldFormCell。相反,它创建了nameLabel并尝试将UITableViewCell注入SELECT Name,LISTAGG(RANK,';') WITHIN GROUP (ORDER BY RANK) AS COMRANK FROM (select name, rank, rank() over (partition by name order by rank) rnk from Table1 ) where rnk < 10; --some value GROUP BY Name; ,这导致了异常。

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

以下是此演示的GitHub回购。 https://github.com/hikui/TypedCellDemo

2 个答案:

答案 0 :(得分:1)

另一种替代方式:

在班级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?

答案 1 :(得分:0)

您可以使用此

添加自定义tableview单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellIdentifier = "textFieldFormCell"
        var cell : TextFieldFormCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! TextFieldFormCell?
        if (cell == nil) {

            cell = Bundle.main.loadNibNamed("TextFieldFormCell", owner: nil, options: nil)?[0] as? TextFieldFormCell
        }

        cell?.backgroundColor = UIColor.clear
        cell?.contentView.backgroundColor = UIColor.clear

        return cell!
    }