在搜索栏上点按时,应用程序崩溃

时间:2018-03-21 09:36:32

标签: swift swift3 swift4

点击搜索栏时,应用程序崩溃并出现以下错误:

无法理解为什么?

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法使用标识符ContactCell对单元格进行出列 - 必须为标识符注册nib或类或连接原型故事板中的单元格'

这是我的代码:

import UIKit

class ContactViewController: UITableViewController, UISearchResultsUpdating {

    var dataSource: [Contact] = []
    var filteredResult = [Contact]()
    let searchController = UISearchController(searchResultsController: nil)

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

    func downloadJSONUrl() {
        let urlString = "https://us-central1-practo-contacts-sample.cloudfunctions.net/get"
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response , error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                if let NameArray = jsonObj?.value(forKey: "contacts") as? [[String: Any]] {
                    for names in NameArray {
                        var cont = Contact()
                        if let name = names["name"] as? String {
                            cont.name = name
                        }
                        if let ph = names["number"] as? String {
                            cont.phoneNumber = ph
                        }
                        self.dataSource.append(cont)
                    }
                    self.dataSource.sort {$0.name.lowercased() < $1.name.lowercased()}
                }
                OperationQueue.main.addOperation {
                    self.tableView.reloadData()
                }
            }
        }).resume()
    }

    // MARK: - Table view data source

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as UITableViewCell
        let contact = self.dataSource[indexPath.row]
        cell.textLabel?.text = contact.name
        cell.detailTextLabel?.text = contact.phoneNumber
        return cell
    }

    func updateSearchResults(for searchController: UISearchController) {
        if searchController.searchBar.text! == "" {
            filteredResult = dataSource
        } else {
            filteredResult = dataSource.filter { $0.name.lowercased().contains(searchController.searchBar.text!.lowercased()) }
        }
        self.tableView.reloadData()
    }

   @IBAction func unwindToContactList(segue: UIStoryboardSegue) {
        guard let viewController =  segue.source as? AddOrEditViewController else { return }
        if let name = viewController.nameTextField.text, let phoneNumber =  viewController.phoneNumberTextField.text {
            let contact =  Contact(name: name, phoneNumber: phoneNumber)
            self.dataSource.append(contact)
            tableView.reloadData()
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "contactDetailsSegue" {
            guard let viewController = segue.destination as? ContactDetialsViewController else {
                return
            }
            guard let indexPath = tableView.indexPathForSelectedRow else { return }
            let contact = self.dataSource[indexPath.row]
            viewController.contact = contact
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您确定在故事板上设置了单元格的标识符,并且标识符名称与代码“ContactCell”上使用的相同吗?