swift - 出列可重复使用的单元格总是崩溃

标签 swift model-view-controller contactpicker

在此代码中,我添加了程序化 VC:

@IBAction func addPerson(_ sender: UIBarButtonItem) {
        let controller = CNContactPickerViewController()
        controller.delegate = self
        navigationController?.present(controller, animated: true, completion: nil)
    }

在这里,当我点击一个联系人时,我会转到另一个 VC(将信息显示到单元格中,然后我想为电话/电子邮件制作一个复选框以在我的第一个 VC 中显示)

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        let vc = EditContactViewController()
        self.navigationController?.pushViewController(vc, animated: true)

我为下一个单元格创建了一个类,并将标识符设置为 Storyboard中的单元格 当我在 tableView cellForRowAtIndexPath 中执行此操作时

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? ConfigCell

return cell!

这给了我:

fatal error: unexpectedly found nil while unwrapping an Optional value

我也试过 withIdentifier,forIndexPath 并且它说:

2016-11-12 01:35:28.626 iEvents3[2637:47714] *** 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'

我删除了重用标识符并将其重新放回去,清理了应用程序,重新启动,一切。

感谢您的宝贵时间!

最佳答案

这可能有几个原因:

  1. 如果您的 UITableViewCell 位于 Controller 本身,请检查重用标识符 是否设置为 "cell"

  2. 如果您的UITableViewCell 有单独的.xib,那么

    a) 在 Controller 的 viewDidLoad() 中用表格 View 注册单元格的 .nib

    self.tableView.register(UINib(nibName:"CustomTableViewCell"), forCellWithReuseIdentifier: "cell")
    

    仅当您确定table viewoutlet 已设置时才注册.nib。否则,如果 outlet if table view 仍然是 nil,它不会注册你的 .nib单元格。

    b) 检查重用标识符是否设置为"cell"

关于swift - 出列可重复使用的单元格总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40557899/

相关文章:

iphone - 单个 Controller 管理模型和多个 View Controller ?

model-view-controller - 如何将参数/参数传递给模型

php - 使用 AJAX、MVC 实现 OOP PHP?

swift - 使用搜索栏时无法选择联系人

swift - 如何仅对新删除的注释进行动画处理?

swift - Xcode:给定自定义类名后, subview Controller 无法正确加载

android - 直接从联系人选择器 Intent 中选择联系人

ios - 如何在单击关闭按钮时从数组中删除数据

ios - 根据类似状态更新表格 View 中使用的单元格中的按钮,而无需重新加载单元格