Search code examples
iosarraysswiftuisearchbaruisearchcontroller

UISearchController filter using two strings


I have been following the tutorial for creating a UISearchController using https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

Everything is set up and working perfectly. I have two categories in my dataArray: Borough and Neighborhood. I set up my searchBar to only look into the values for Neighborhood. I created buttons for the different boroughs and what i'm hoping to achieve is when the button is pressed it will show all the possible values for that borough, but I can't seem to find anything to do this


Solution

  • I believe what you are saying is that you want to search for a text value entered via the search bar in the Neighbourhood values and to specifically look for the Neighbourhood in a given Borough. Correct?

    If that is the case, all you'd have to do is modify your search condition. If your search condition is currently something like (to borrow from the rw.com tutorial):

    return row.neighbourhood.lowercaseString.containsString(searchText.lowercaseString)
    

    Then you'd have to modify the condition to include a check for the Borough name too - and here, we'll assume the borough variable contains the Borough name that you selected via button press:

    return row.neighbourhood.lowercaseString.containsString(searchText.lowercaseString) && row.borough == borough
    

    That should return only array values which have the specified Neighbourhood name and are in the given Borough. If that is not what you wanted, then you might want to provide a bit more detail such as the structure of the data array, how the Neighbourhoods and Boroughs are linked, and perhaps even a screenshot of the UI so that we can see how you select a Borough in the UI.