ios - swift 4 : Could not load NIB in bundle: 'NSBundle' for custom UITableViewCell (no IB)

标签 ios swift xcode uitableview

我已经构建了自己的 UITableViewCell (完全通过代码,没有界面生成器),并且我遇到了以下异常:

[...] NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle [...]

我相当确定我不需要使用 awakeFromNib() (参见代码),但这是我将其作为“不可重用”传递时使其工作的唯一方法

// this works
func tableView(_ tableView: UITableView,
  cellForRowAt indexPath: IndexPath [...] {
    return myCustomCell() 

// this fails
tableView.register(MyCustomCell.self, forCellReuseIdentifier: "foo")
//throws immediately after this line: "[...] Could not load NIB in bundle"

我的自定义单元格:

class TextValueTableViewCell: UITableViewCell {
    let textField: UITextField = UITextField(
        frame: CGRect(x: 30, y: 5, width: 350, height: 25)
    )
    let label = UILabel(frame: CGRect(x: 30, y: 5, width: 350, height: 25))


    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.awakeFromNib();
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.label.text = "oh"
        self.contentView.addSubview(self.label)
        self.contentView.addSubview(self.textField)
        self.textField.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -25).isActive = true
        self.textField.translatesAutoresizingMaskIntoConstraints = false;
        self.textField.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor, constant: 0).isActive = true
        self.textField.widthAnchor.constraint(equalTo: self.label.widthAnchor, constant: 0).isActive = true
        self.textField.textAlignment = .right;
        self.textField.placeholder = "Account Name"
    }



    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

}

正如我所说,我没有使用界面生成器(并且我没有计划使用它),所以我不确定为什么它会尝试从 bundle 中加载某些内容。具体来说,我不是必须将其注册为 NIB 吗?

 tableView.register(<#T##nib: UINib?##UINib?#>, forCellReuseIdentifier: <#T##String#>)

最佳答案

class TextValueTableViewCell: UITableViewCell {

    let textField: UITextField = UITextField(
        frame: CGRect(x: 30, y: 5, width: 350, height: 25)
    )

    let label = UILabel(frame: CGRect(x: 30, y: 5, width: 350, height: 25))

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        setupCell()
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
    }

    func setupCell() {
        label.text = "oh"
        contentView.addSubview(label)
        contentView.addSubview(textField)
        textField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -25).isActive = true
        textField.translatesAutoresizingMaskIntoConstraints = false;
        textField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
        textField.widthAnchor.constraint(equalTo: label.widthAnchor, constant: 0).isActive = true
        textField.textAlignment = .right;
        textField.placeholder = "Account Name"
    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

关于ios - swift 4 : Could not load NIB in bundle: 'NSBundle' for custom UITableViewCell (no IB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086169/

相关文章:

ios - UIImageView 不动

ios - 仅允许一个 Controller 旋转

ios - UIScrollView 中的 subview 对齐错误

iphone - 我可以在没有设备的情况下从 iTunes 测试应用程序吗?

swift - 为什么我的 NSWindow 有一个工具栏?我怎样才能让它消失?

iOS 嵌套 UITableView 不必要地加载所有单元格

ios - 如何根据尺寸类别在启动屏幕中显示不同的图像

ios - 在iOS中使用MVVM时如何在不同片段之间进行通信

objective-c - Facebook 登录对话框不适用于 UIAlertView,但适用于 UIButton

ios - 在同一 View Controller 中对多个 Collection View 或 TableView 实现 3D 触摸