Commit 5c06c519 by Javier Heisekce

al borrar el buscador sigue funcionando

parent 3f6e618b
......@@ -14,6 +14,7 @@ class ContactsViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var contactsTable: UITableView!
var contactStructArray = [ContactStruct]()
var contactAux = [ContactStruct]()
var contactStruct : ContactStruct?
let contactStore = CNContactStore()
......@@ -27,6 +28,7 @@ class ContactsViewController: UIViewController {
}
func importContacts() {
self.contactStructArray = []
let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any]
let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.givenName
......@@ -50,10 +52,10 @@ class ContactsViewController: UIViewController {
auxLetter = String(contact.givenName.prefix(1)) //auxLetter tiene la primera letra del nombre
isNotTheFirst = true //la siguiente vez que las letras no sean iguales guardara en el array
}
}
}
self.contactStructArray.append(ContactStruct(isExpanded: true, contacts: contacts, letter: auxLetter)) //agregamos el ultimo nombre
self.contactAux = self.contactStructArray
}
catch {
print("unable to fetch contacts")
......@@ -152,21 +154,26 @@ extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate,
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
importContacts()
contactStructArray = contactAux
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
var foundContacts = [ContactStruct]()
if searchText == "" {
importContacts()
contactStructArray = contactAux
contactsTable.reloadData()
} else {
for indexContact in contactStructArray.indices{
let contactFound = contactStructArray[indexContact].contacts.filter({$0.givenName.lowercased().contains(searchText.lowercased())})
for indexContact in contactAux.indices{
let contactFound = contactAux[indexContact].contacts.filter(
{
let fullName = "\($0.givenName) \($0.familyName)"
return fullName.lowercased().contains(searchText.lowercased())
}
)
if !contactFound.isEmpty {
foundContacts.append(ContactStruct(isExpanded: true, contacts: contactFound, letter: contactStructArray[indexContact].letter))
foundContacts.append(ContactStruct(isExpanded: true, contacts: contactFound, letter: contactAux[indexContact].letter))
}
}
contactStructArray = foundContacts
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment