Ios SearchBar没有将我的搜索返回到联系人

Ios SearchBar没有将我的搜索返回到联系人,ios,swift,Ios,Swift,我目前正在使用Xcode 10和Swift 4.2,我试图使用搜索栏搜索我的联系人列表,除了搜索栏返回超出范围的索引外,所有操作都已完成。这是我的密码: @IBOutlet weak var searchBar: UISearchBar! var contactList: [CNContact]! var inSearchMode = false var filteredData = [CNContact]() override func viewDidLoad() { super.

我目前正在使用Xcode 10和Swift 4.2,我试图使用搜索栏搜索我的联系人列表,除了搜索栏返回超出范围的索引外,所有操作都已完成。这是我的密码:

@IBOutlet weak var searchBar: UISearchBar!

var contactList: [CNContact]!
var inSearchMode = false
var filteredData = [CNContact]()

override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    searchBar.returnKeyType = UIReturnKeyType.done
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let store = CNContactStore()
    store.requestAccess(for: .contacts, completionHandler: { (success, error) in
        if success {
            let keys = CNContactViewController.descriptorForRequiredKeys()
            let request = CNContactFetchRequest(keysToFetch: [keys])
            do {
                self.contactList = []
                try store.enumerateContacts(with: request, usingBlock: { (contact, status) in
                    self.contactList.append(contact)
                })
            } catch {
                print("Error")
            }
            OperationQueue.main.addOperation({
                self.tableView.reloadData()
            })
        }
    })
}
昨晚我在stack上做了search func,除了运行应用程序时抛出的错误之外,一切都很好。我怎样才能解决它

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if contactList != nil {
        return contactList.count
    }
    if inSearchMode {
        return filteredData.count
    }
    return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
    let contact: CNContact!

    contact = contactList[indexPath.row]
    cell.textLabel?.text = "\(contact.givenName) \(contact.familyName)"

    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let contact = contactList[indexPath.row]
    let controller = CNContactViewController(for: contact)
    navigationController?.pushViewController(controller, animated: true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
        inSearchMode = false
        view.endEditing(true)
        tableView.reloadData()
    } else{
       inSearchMode = true
       filteredData = contactList.filter {
            $0.givenName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil ||
            $0.familyName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil
        }
       tableView.reloadData()
    }
}
您应该检查您是否处于搜索模式或不在cellForRow中才能正确获取

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
    return filteredData.count
}
if contactList != nil {
    return contactList.count
}
return 0
}
当您调用reloadData()-numberOfRows-CellForRow时,也会调用

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
    return filteredData.count
}
if contactList != nil {
    return contactList.count
}
return 0
}