筛选联系人 Returns 混合联系人信息 - Swift
Filtering Contacts Returns Mixed Contact Information - Swift
我是一名新 Swift 开发人员。我已经成功地从 phone 中提取联系人并将它们保存在一个联系人数组中,每个联系人都具有以下结构:
struct Contact {
var contactDetails:CNContact
var isFavorite:Bool
数组名为 deviceContacts。
我还设置了一个名为 searchResults 的空联系人数组。
我在联系人 table 中有一个搜索栏,我想在您键入时自动过滤联系人。我可以使用以下搜索栏代码成功完成此操作:
extension ContactsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchResults = deviceContacts.filter{([=13=].contactDetails.familyName.prefix(searchText.count) == searchText)}
searching = true
tableView.reloadData()
// This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.
}
}
尽管此功能会在您键入时进行搜索,并在您键入更多字符时实时过滤结果,但结果会混淆。他们显示正确的姓氏,但显示错误的名字。名字从列表的顶部开始。因此,例如,如果您在模拟器中的标准联系人中搜索 "Bell",您将找到 Bell, John。 Bell 是名单中的第二个人,John 是名单中的第一个人。
我尝试为此使用谓词搜索联系人,但无法像此功能那样在您实时输入时进行搜索。
有没有办法在搜索的时候拉全联系人?或者仅提取对匹配的联系人的引用并将该联系人放入 searchResults 数组的方法?还是一种使用谓词方法在您键入时进行搜索的方法?
如有任何帮助,我们将不胜感激。
在 cellForRowAt 中,我犯了一个错误,没有更改对名字的 searchResults 的引用,只更改了姓氏。这是 tableView 的代码:
if searching {
cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
} else {
cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName
}
我是一名新 Swift 开发人员。我已经成功地从 phone 中提取联系人并将它们保存在一个联系人数组中,每个联系人都具有以下结构:
struct Contact {
var contactDetails:CNContact
var isFavorite:Bool
数组名为 deviceContacts。
我还设置了一个名为 searchResults 的空联系人数组。
我在联系人 table 中有一个搜索栏,我想在您键入时自动过滤联系人。我可以使用以下搜索栏代码成功完成此操作:
extension ContactsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchResults = deviceContacts.filter{([=13=].contactDetails.familyName.prefix(searchText.count) == searchText)}
searching = true
tableView.reloadData()
// This does not work. It filters by the last name. But it keeps John as the first name, etc. It does not pull the full contact.
}
}
尽管此功能会在您键入时进行搜索,并在您键入更多字符时实时过滤结果,但结果会混淆。他们显示正确的姓氏,但显示错误的名字。名字从列表的顶部开始。因此,例如,如果您在模拟器中的标准联系人中搜索 "Bell",您将找到 Bell, John。 Bell 是名单中的第二个人,John 是名单中的第一个人。
我尝试为此使用谓词搜索联系人,但无法像此功能那样在您实时输入时进行搜索。
有没有办法在搜索的时候拉全联系人?或者仅提取对匹配的联系人的引用并将该联系人放入 searchResults 数组的方法?还是一种使用谓词方法在您键入时进行搜索的方法?
如有任何帮助,我们将不胜感激。
在 cellForRowAt 中,我犯了一个错误,没有更改对名字的 searchResults 的引用,只更改了姓氏。这是 tableView 的代码:
if searching {
cell.textLabel?.text = searchResults[indexPath.row].contactDetails.familyName+", "+searchResults[indexPath.row].contactDetails.givenName
} else {
cell.textLabel?.text = deviceContacts[indexPath.row].contactDetails.familyName+", "+deviceContacts[indexPath.row].contactDetails.givenName
}