【问题标题】:'NSInternalInconsistencyException' reason: 'Could not load NIB in bundle' when dequeuing tableview cell'NSInternalInconsistencyException' 原因:'Could not load NIB in bundle' 出列 tableview 单元格时
【发布时间】:2018-12-26 19:08:49
【问题描述】:

我不知道为什么会收到此错误,并且我已尝试查看其他答案,但找不到解决方案。

有问题的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    messagesTableView?.register(UINib.init(nibName: "messageTableViewCell", bundle: nil), forCellReuseIdentifier: "messageTableViewCell")

    messagesTableView.delegate = self;
    messagesTableView.dataSource = self;
    dataSrc.delegate = self


}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if let cell = tableView.dequeueReusableCell(withIdentifier: "messageTableViewCell", for: indexPath) as? MessagesTableViewCell {

        cell.configureWithItem(item: dataArray[indexPath.item])

        return cell
    }
    return UITableViewCell()
}

当调用 tableView.dequeueReusableCell 时,我得到了错误。我还确保单元格的标识符设置为“messageTableViewCell”

【问题讨论】:

  • 请确保您的笔尖名称为MessageTableViewCell
  • 我是个白痴,我没有 nib 文件。谢谢
  • 没问题,会发生的

标签: ios swift uinib


【解决方案1】:

请确保你已经创建了一个名为MessageTableViewCellnib/xib文件,然后注册nib文件MessageTableViewCell

let nib = UINib(nibName: "MessageTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "messageTableViewCell")

【讨论】:

    【解决方案2】:

    当你初始化UINib,作为nibName参数你必须传递文件名

    UINib(nibName: "TableViewCell", bundle: nil)
    

    所以在您的情况下,我想您的文件与您的班级名称相同 MessagesTableViewCell

    messagesTableView?.register(UINib(nibName: "MessagesTableViewCell", bundle: nil), forCellReuseIdentifier: "messageTableViewCell")
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多