【问题标题】:Swift 4: Could not load NIB in bundle: 'NSBundle' for custom UITableViewCell (no IB)Swift 4:无法在包中加载 NIB:自定义 UITableViewCell 的“NSBundle”(无 IB)
【发布时间】:2019-05-11 00:09:11
【问题描述】:

我已经构建了自己的 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#>)

【问题讨论】:

  • 为什么要手动拨打awakeFromNib()?这是nib的一种方法。
  • 当我传递我的自定义单元格“return myCustomCell()”时,awakeFromNib() 似乎没有被调用 -> 但我可能没有正确使用它:)
  • 删除awakeFromNib 函数。将该代码的其余部分移至“设置”函数,super.awakeFromNib() 行除外。

标签: ios swift xcode uitableview


【解决方案1】:
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)
    }

}

【讨论】:

  • 谢谢你 - 这样做了,但它仍然抛出同样的错误。 :(顺便说一句,这似乎有效:
  • 顺便说一句,这似乎有效:让 testView = UITableView(); testView.register(TextValueTableViewCell.self, forCellReuseIdentifier: "foo") - 所以我相信它可能与我的 UITableView 的设置方式有关? (类 UITableViewNavigation: UITableViewController { ... })
【解决方案2】:

例如,如果您使用 UIViewController 类名作为 TextPicker 然后你需要像 Bellow Code 一样使用

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

如果你在其他项目中使用 TableView 和 Xib 作为框架

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

【讨论】:

    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多