Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift UISearchBar Scope Issue

Hello! I am a beginner here trying to learn coding.

I added a UISearchBar in my iOS project and want the scope toggle to change the data type the search bar searches by. As an example I have some sample data for Country which contains properties of Name and Number. What I want to do is have the search bar search by Name when the scope is toggled at Country Name and the search bar to change the search criteria to Number when the toggle is at Country Number.

I listed my code below and I am missing a method or perhaps more but I am stuck and I don't know what to do after reviewing for a day.

Really appreciate any help.

Erik

Example:

class CountryTableViewController: UITableViewController, UISearchResultsUpdating { func updateSearchResultsForSearchController(searchController: UISearchController) {

    let searchBar = searchController.searchBar
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
    filterContentForSearchText(searchController.searchBar.text!, scope:  scope)
}
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}

// MARK: Properties
let searchController = UISearchController(searchResultsController: nil)

var countries = [Country]()
var filteredTableData = [Country]()

//func filterContentForSearchText(searchText: String, scope: String){

override func viewDidLoad() { super.viewDidLoad()

    // Load the sample data.
    loadSamplePurchaseOrder()

    // Search Controller
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.scopeButtonTitles = ["Country No.", "Country"]
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    self.tableView.reloadData()
}

func loadSampleCountries(){
       let country1 = country(coNo: "H14567", countryName: "USA")
       let country2 = country(coNo: "H14568", countryName: "CANADA")
  countries += [country1, country2]
}