I have made a very simple address book which shows the picture, name and the phone number of the user. And I have implemented them in a table view, now I have added a search bar for the table and it does not behave as I want!!
my table works fine. I have searched the whole stack overflow and internet but none of them was about searching through CNContatcs.
var twoDimensionalArray = [ExpandableNames]()
var emptySearchBAR = [ExpandableNames]()
struct FavoritableContact {
let contact: CNContact
}
struct ExpandableNames {
var isExpanded: Bool
var names: [FavoritableContact]
}
searchBar delegate:
func searchBar(_ searchBar: UISearchBar, textDidChange
searchText: String) {
guard !searchText.isEmpty else { twoDimensionalArray =
emptySearchBAR
tableView.reloadData()
return }
twoDimensionalArray = emptySearchBAR.filter({ (element) -> Bool in
guard let text = searchBar.text else { return false }
print("element = \(element.names[0].contact)")
return element.names[0].contact.givenName.contains(text)
})
tableView.reloadData()
}
table view:
let favoritableContact = twoDimensionalArray[indexPath.section].names[indexPath.row]
cell?.phoneNumber.text = favoritableContact.contact.phoneNumbers.first?.value.stringValue
my problem is with this part:
return element.names[0].contact.givenName.contains(text)
as it is a CNcontacts , I'm a bit confuse in search through it. it is returning the first name :|