如何在 swift 中搜索 phone 个联系人

How to search phone contacts in swift

在项目中我正在获取我所有的联系人..在这里我需要通过他们的名字搜索联系人如何做到这一点

我几乎完成了但无法在 textDidChange

中过滤

下面是我试过的代码:

class ContactsViewController1: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var joinersTableView: UITableView!
    var contacts = [CNContact]()

    var search = false
    var searchArray = [CNContact]()

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return jsonArrayTagged.count
        } else {
            if search {
                return searchArray.count
            } else {
                return contacts.count
            }
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 {
            var cell1: ContactsTableViewCell2 = tableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell2", for: indexPath) as! ContactsTableViewCell2
            if search {
                cell1.nameLbl.text    = searchArray[indexPath.row].givenName + " " + searchArray[indexPath.row].familyName
                cell1.empRoleLbl.text = searchArray[indexPath.row].phoneNumbers.first?.value.stringValue
                cell1.inviteButn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
            } else {
                cell1.nameLbl.text    = contacts[indexPath.row].givenName + " " + contacts[indexPath.row].familyName
                cell1.empRoleLbl.text = contacts[indexPath.row].phoneNumbers.first?.value.stringValue
                cell1.inviteButn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
            }

            return cell1
        }

        return UITableViewCell()
    }
}

extension ContactsViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchArray = contacts.filter({[=12=].lowercased().prefix(searchText.count) == searchText.lowercased()})
        search = true

        joinersTableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        search = false
        searchBar.text = ""
        joinersTableView.reloadData()
    }
}

错误:

Value of type 'CNContact' has no member 'lowercased'

您不能只将 CNContact 用作 String 并将其与 String 进行比较。您需要指定要过滤 CNContact 中的 String 属性。

如果您想搜索 familyName,请执行 [=17=].familyName.lowerCased() 而不是 [=18=].lowerCased,因为 [=19=]CNContact.

extension ContactsViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchArray = contacts.filter {[=10=].familyName.lowercased().prefix(searchText.count) == searchText.lowercased()}
        search = true

        joinersTableView.reloadData()
    }
...
}

与你的问题无关,但你为什么只搜索文本的开头?使用 localizedCaseInsensitiveContains 而不是 prefix 会产生更好的用户体验。

您不需要使用 search: Bool.

试试这些代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return jsonArrayTagged.count
    } else {
        return searchArray.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 1 {
        var cell1: ContactsTableViewCell2 = tableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell2", for: indexPath) as! ContactsTableViewCell2
        cell1.nameLbl.text    = searchArray[indexPath.row].givenName + " " + searchArray[indexPath.row].familyName
        cell1.empRoleLbl.text = searchArray[indexPath.row].phoneNumbers.first?.value.stringValue
        cell1.inviteButn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
        return cell1
    }

    return UITableViewCell()
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty{
        searchArray = contacts
        joinersTableView.reloadData()
    }else{
        searchArray = contacts.filter({[=10=].familyName.lowercased().contains(searchText.lowercased()) || [=10=].middleName.lowercased().contains(searchText.lowercased()) || [=10=].givenName.lowercased().contains(searchText.lowercased())})
        joinersTableView.reloadData()
    }
}
    
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.text = ""
    searchArray = contacts
    joinersTableView.reloadData()
}