0

I have an array of struct's which each have a title and subtitle:

struct searchItem {
    var title: String
    var subtitle: String
}

let itemArray: [searchItem] = [
        searchItem(title: "Bob", subtitle: "Man"),
        searchItem(title: "Susan", subtitle: "Woman"),
        searchItem(title: "Joe", subtitle: "Man")
]

var filteredArray = [searchItem]()

Each searchItem's title and subtitle are used to create a tableViewCell in a tableViewController, with a UISearchBar at the top:

simulator image of tableView

I need to somehow filter the itemArray based on the search term, and each searchItem's title and subtitle, So either the search term "Man", or "Bob" will return the individual Bob.

How does one go about doing this? Thanks in advance!

2
  • How much do you have for the searching? Have you implemented UISearchController yet? Or do you just need a way to filter it? Commented Apr 3, 2016 at 22:22
  • I've created a UISearchController, and currently have an empty updateSearchResultsForSearchController function Commented Apr 3, 2016 at 22:52

2 Answers 2

2

You can filter your array with filter method of swift array. When you use the filter it returns and another array of items depends on your searchText. So you should add searched items to your new filteredArray and reload your tableView.

if let searchBarText = searchController.searchBar.text{
            let searchText = searchBarText.lowercaseString
            filteredArray = self. itemArray.filter({$0.title.lowercaseString.rangeOfString(searchText) != nil})
            filteredArray += self.itemArray.filter({$0. subtitle.lowercaseString.rangeOfString(searchText) != nil})
            tableView.reloadData()
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but how can I do this filtering for both the title, and subtitle string? If I just repeat the action for both it will return any searchItems that satisfy BOTH strings, rather than EITHER
It's simple mate, just filter again with the subtitle strings and add them to your filtered array. I updated my answer for your case.
Perfect, thank you! I was afraid that I might get duplicates if I filtered both, but I realized that would only happen if I had an item whose title was the same as another item's subtitle, which isn't the case.
Have a nice coding :)
1

Swift 3 version :

if let searchBarText = searchController.searchBar.text{
            let searchText = searchBarText.lowercaseString
            filteredArray = categoryArray.filter ({$0.title.lowercased().range(of: searchText.lowercased()) != nil})
            filteredArray += categoryArray.filter ({$0.sybtitle.lowercased().range(of: searchText.lowercased()) != nil})
            tableView.reloadData()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.