0

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 :|

jack
  • 77
  • 2
  • 8
  • If I understand your code correctly.. emptySearchBar is an array of all of your contacts. Once you have this array filled up, CNContacts has done its job. This is only a matter of your tableview. You are setting the cell.phoneNumber.text to the first name in this line... cell?.phoneNumber.text = favoritableContact.contact.phoneNumbers.first?.value.stringValue – Mocha Jan 07 '19 at 20:54
  • whenever the search bar is empty and no text is entered, I use emptySearchBar . – jack Jan 07 '19 at 20:57
  • and for cell?.phoneNumber.text = favoritableContact.contact.phoneNumbers.first?.value.stringValue, that is becuase I only need the first phone number of each contact – jack Jan 07 '19 at 20:58
  • Sorry, my mistake. So does your twoDimensionalArray only have an array of first name strings? What do you mean by it is returning the first name. – Mocha Jan 07 '19 at 21:00
  • twoDimensionalArray has 2 value ,one of them is names, and names is a CNContacts, which means it has the first name, family name, phone number and... – jack Jan 07 '19 at 21:02
  • whenever usaer enter something in to the search bar I should create the twoDimensionalArray – jack Jan 07 '19 at 21:03
  • Ah, so your filter is not working. Have you put a breakpoint in it? – Mocha Jan 07 '19 at 21:03
  • i have done that before, and my problem is with return element.names[0].contact.givenName.contains(text) – jack Jan 07 '19 at 21:07
  • I think you need to debug your array to see if the name actually does match, po your emptySearchBAR and see if a value contains the text that is being passed in. – Mocha Jan 07 '19 at 21:12
  • that is not passed! what number is in [ ] , the name related to that number is related – jack Jan 07 '19 at 21:13
  • Sorry-- I don't understand. What do you mean that is not passed, what number is in []? Based on the code, you are passing in the searchbar text and filtering the contacts based on the contacts given name. – Mocha Jan 07 '19 at 21:16

0 Answers0