无法使标识符单元格 XX 错误的单元格出列 - 无法确定如何解决

unable to dequeue cell with identifier cell XX error - Cannot figure how to solve

我目前在我的 swift 文件中遇到一个错误,关于这个主题的其他线程没有解决任何问题。

感谢我的 NewsTableViewController,我创建了一个表格视图,我为我的自定义单元格创建了一个原型,我将其标识为 cell 在主故事板中。 Controller 是单独的,没有 segue 链接,就像这样显示的原型:

Controller on the right is my table view controller that bugs

但是当我执行应用程序时,当我单击启动实例化 NewsTableViewController 的代码片段的按钮时,出现错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我的原型标识符还没有问题,怎么了? 这是我的 NewsTableViewController 的代码:

class NewsTableViewController: UITableViewController {

var listOfDisplays = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listOfDisplays.count
}

func fetchEventsNames()
{
    let fetchRequest:NSFetchRequest<Events> = Events.fetchRequest()
    do {
        let results = try DataBaseControler.getContext().fetch(fetchRequest)
        for i in results as [Events]{ /* *dewraper les arguments (i.name)!*/
            listOfDisplays.append(i.title!)
        }
    }
    catch
    {
        print("errors\(error)")
    }
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! NewsTableViewCell
    cell.cellImage.image = UIImage(named: "rcbl.jpg")
    cell.cellLabel.text = listOfDisplays[indexPath.row]
    return(cell)
}

这也是我的NewsTableViewCells的代码:

class NewsTableViewCell: UITableViewCell {

@IBOutlet weak var cellLabel: UILabel!
@IBOutlet weak var cellImage: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

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

    // Configure the view for the selected state
}

}

然后这是我调用构造函数的代码片段:

...
    switch (names[row])
        {
    case "Actualités":
        let destination = NewsTableViewController() // Your destination
        destination.listOfDisplays = ["test1", "TEST2"]
        navigationController?.pushViewController(destination, animated: true)...

我应该怎么做才能解决这个问题?提前谢谢你们。

当您像这样创建视图控制器时:

let destination = NewsTableViewController() // Your destination

您只是使用您在故事板中完成的设置的 none 创建 class 的实例。

您要么需要使用如下内容从情节提要中实例化一个:

let storyboard = UIStoryboard(name: "Main", bundle: nil) // Use the appropriate story board name
let destination = storyboard.instantiateViewController(withIdentifier: "testtable") // Make sure you have allocated the correct storyboard id to the view controller
self.navigationController?.pushViewController(destination, animated: true)

或者您可以像这样在 viewDidLoad() 中为 table 视图注册单元格 class:

self.tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "testcell") // Use the correct class and identifier.