Commit 5c06c519 by Javier Heisekce

al borrar el buscador sigue funcionando

parent 3f6e618b
...@@ -14,6 +14,7 @@ class ContactsViewController: UIViewController { ...@@ -14,6 +14,7 @@ class ContactsViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var contactsTable: UITableView! @IBOutlet weak var contactsTable: UITableView!
var contactStructArray = [ContactStruct]() var contactStructArray = [ContactStruct]()
var contactAux = [ContactStruct]()
var contactStruct : ContactStruct? var contactStruct : ContactStruct?
let contactStore = CNContactStore() let contactStore = CNContactStore()
...@@ -27,6 +28,7 @@ class ContactsViewController: UIViewController { ...@@ -27,6 +28,7 @@ class ContactsViewController: UIViewController {
} }
func importContacts() { func importContacts() {
self.contactStructArray = []
let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any] let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any]
let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor]) let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.givenName request.sortOrder = CNContactSortOrder.givenName
...@@ -50,10 +52,10 @@ class ContactsViewController: UIViewController { ...@@ -50,10 +52,10 @@ class ContactsViewController: UIViewController {
auxLetter = String(contact.givenName.prefix(1)) //auxLetter tiene la primera letra del nombre 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 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.contactStructArray.append(ContactStruct(isExpanded: true, contacts: contacts, letter: auxLetter)) //agregamos el ultimo nombre
self.contactAux = self.contactStructArray
} }
catch { catch {
print("unable to fetch contacts") print("unable to fetch contacts")
...@@ -152,21 +154,26 @@ extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate, ...@@ -152,21 +154,26 @@ extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate,
} }
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
importContacts() contactStructArray = contactAux
} }
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
var foundContacts = [ContactStruct]() var foundContacts = [ContactStruct]()
if searchText == "" { if searchText == "" {
importContacts() contactStructArray = contactAux
contactsTable.reloadData() contactsTable.reloadData()
} else { } else {
for indexContact in contactStructArray.indices{ for indexContact in contactAux.indices{
let contactFound = contactStructArray[indexContact].contacts.filter({$0.givenName.lowercased().contains(searchText.lowercased())}) let contactFound = contactAux[indexContact].contacts.filter(
{
let fullName = "\($0.givenName) \($0.familyName)"
return fullName.lowercased().contains(searchText.lowercased())
}
)
if !contactFound.isEmpty { 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 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