Swift 4:无法在捆绑包“ NSBundle”中为自定义UITableViewCell加载NIB(无IB)

时间:2019-05-11 00:09:11

标签: ios swift xcode uitableview

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

  

[...] NSInternalInconsistencyException”,原因:“无法在其中加载NIB   捆绑包:“ 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)!
    }

}

正如我所说,我没有使用过接口构建器(并且我没有计划),所以我不确定为什么它会尝试从捆绑软件中加载某些东西。具体来说,那我不必将其注册为NIB吗?

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

2 个答案:

答案 0 :(得分:0)

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)
    }

}

答案 1 :(得分:0)

例如,如果您使用UIViewController类名称作为TextPicker 然后,您需要使用类似波纹管代码

      let bundle = Bundle(for: TextPicker.self)
      super.init(nibName: "TextPicker" , bundle: bundle)

如果您在其他项目中将TableView和Xib用作Framework

      tableView.register(UINib.init(nibName: "TextPickerCell", bundle: bundle), forCellReuseIdentifier: "TextPickerCell")
  let bundle = Bundle(for: TextPicker.self)