Swift 4.2 使用联系人框架通过搜索栏搜索联系人
Swift 4.2 Search contacts with Search Bar using Contacts Framework
我目前正在开发一个联系人应用程序,只是为了测试一些功能,但我被搜索栏困住了。我无法在主页中的所有联系人之间进行搜索。 Swift 4.2 和 Xcode 10
class ContactsViewController: UITableViewController, CNContactViewControllerDelegate, UISearchBarDelegate {
// Outlet for Search Bar
@IBOutlet weak var searchBar: UISearchBar!
这是我的代理在 IBOutlet 上的定义
然后我的功能是在主页上显示联系人
/ * Show Contacts *
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if contactList != nil {
return contactList.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)
}
如何使用 searchBar 将 Name 或 Surname 作为关键字来查看我的联系人。
这是我的尝试之一,但我收到错误 contains
:
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({[=13=].contains(searchBar.text!)})
tableView.reloadData()
}
}
您的 contactList
数组包含 CNContact
个实例。所以你的 filter
中的 [=13=]
是 CNContact
。 contains
失败,因为 CNContact
没有 contains
方法。
想一想如果您只有一个 CNContact
变量并且想查看联系人的姓名是否包含搜索文本,您需要写什么。
而且您可能不希望 contains
因为您可能想要进行不区分大小写、不区分变音符号的搜索。
下面是查看联系人的名字和姓氏属性的示例。根据需要添加其他属性:
filteredData = contactList.filter {
[=10=].givenName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil ||
[=10=].familyName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil
}
我目前正在开发一个联系人应用程序,只是为了测试一些功能,但我被搜索栏困住了。我无法在主页中的所有联系人之间进行搜索。 Swift 4.2 和 Xcode 10
class ContactsViewController: UITableViewController, CNContactViewControllerDelegate, UISearchBarDelegate {
// Outlet for Search Bar
@IBOutlet weak var searchBar: UISearchBar!
这是我的代理在 IBOutlet 上的定义
然后我的功能是在主页上显示联系人
/ * Show Contacts *
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if contactList != nil {
return contactList.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)
}
如何使用 searchBar 将 Name 或 Surname 作为关键字来查看我的联系人。
这是我的尝试之一,但我收到错误 contains
:
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({[=13=].contains(searchBar.text!)})
tableView.reloadData()
}
}
您的 contactList
数组包含 CNContact
个实例。所以你的 filter
中的 [=13=]
是 CNContact
。 contains
失败,因为 CNContact
没有 contains
方法。
想一想如果您只有一个 CNContact
变量并且想查看联系人的姓名是否包含搜索文本,您需要写什么。
而且您可能不希望 contains
因为您可能想要进行不区分大小写、不区分变音符号的搜索。
下面是查看联系人的名字和姓氏属性的示例。根据需要添加其他属性:
filteredData = contactList.filter {
[=10=].givenName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil ||
[=10=].familyName.range(of: searchBar.text!, options: [.caseInsensitive, .diacriticInsensitive ]) != nil
}