Swift 尽管已正确标识单元格,但无法将标识符错误的单元格出列

Swift 尽管已正确标识单元格,但无法将标识符错误的单元格出列,swift,xcode,uitableview,Swift,Xcode,Uitableview,我创建了两个UITableViewCell对象(FriendsCell、AddFriendsCell),并具有以下代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == self.selectedFriends.count { let cell = tableView.dequeu

我创建了两个UITableViewCell对象(FriendsCell、AddFriendsCell),并具有以下代码:

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

    if indexPath.row == self.selectedFriends.count {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AddFriendsCell", for: indexPath) as! AddFriendsCell
        return cell

    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FriendsCell", for: indexPath) as! FriendsCell
     ...
AddFriendsCell有一个按钮,单击该按钮时,应转到另一个视图控制器。但是,当单击时,它会返回记录良好的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'unable to dequeue a cell with
identifier AddFriendsCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
当对应的原型单元在其情节提要设置中被非常清楚地标识为“AddFriendsCell”并且属于AddFriendsCell类(UITableViewCell类)时,我不明白为什么会出现此错误

当我使用以下命令继续第一个dequeueReusableCell行时:

tableview.register(AddFriendsCell, forCellReuseIdentifier: "AddFriendsCell")
结果是,在运行时,它生成一个空白单元格,以代替先前正确格式化的AddTableCell


请帮助我了解按下AddFriendsCell中的此按钮时引发此错误的原因,以及如何更正此错误。

您需要先注册单元格,然后再使用它。例如:

override func viewDidLoad() {
    super.viewDidLoad()

    var nib = UINib(nibName: "AddFriendsCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "AddFriendsCell")

}
更新:如果没有
xib
文件,请使用

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(AddFriendsCell.self, forCellReuseIdentifier: "AddFriendsCell")

}

您说您在故事板中正确设置了原型单元的类。在你的故事板中,你也设置了它的标识符吗?事实上,这个类是AddFriendsCell,标识符是“AddFriendsCell”。不幸的是,确切的代码导致了我以前遇到的一个错误:未捕获异常:“无法在bundle中加载NIB:”…(长路径)…”名为“AddFriendsCell”,试试看,这与您说的错误不同。您通过了类,而不是像我这样的实例。在键入代码并进行测试后,它会给出相同的错误。您是否有
AddFriendsCell.swift
AddFriendsCell.xib
文件?或者只有
AddFriendsCell.swift
?我只有AddFriendsCell.swift。事实上,在整个项目中,我在navigator中没有看到任何.xib文件。