How to use the visibility panel with UISearchController

I can create a visibility panel for the UISearchController and set their names

resultSearchController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.searchBar.showsScopeBar = true controller.searchBar.scopeButtonTitles = ["One", "two", "three"] controller.searchBar.sizeToFit() self.tableView.tableHeaderView = controller.searchBar return controller })() 

But how do I really sort using the value bar. My current sorting method is as follows

 func updateSearchResultsForSearchController(searchController: UISearchController) { filteredTableData.removeAll(keepCapacity: false) let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text) let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate) filteredTableData = array as! [String] tableView.reloadData() } 
+7
ios uitableview swift uisearchcontroller
source share
2 answers

You need to add a delegate to the search bar as follows.

 class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate { 

Save case delegate in view loaded as follows

 resultSearchController.searchBar.delegate = self 

The delegate has a way to tell you which area pane button is pressed as follows

  func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { filterArrayWithCharacterLength(selectedScope) } 

See my tutorial on How to add a UISearchController to tableView for details.

0
source share

Even if it's late, it can help someone! Suppose you have a Person object and an array of filtered people in which you store the people you filtered, and then

 struct Person { let name:String } func updateSearchResultsForSearchController(searchController:UISearchController) { //Start filtering only when user starts to write something if searchController.searchBar.text.length > 0 { filteredPeople.removeAll(keepCapacity: false) self.filterContentForSearchText(searchController.searchBar.text) self.tableView.reloadData() } else { self.filteredPeople = self.people self.tableView.reloadData() } } func filterContentForSearchText(searchText: String) { self.filteredPeople = self.people.filter({( person: Person) -> Bool in //In this case we are searching for all, and the search is case insensitive let categoryMatch = (scope == "All") let stringMatch = person.name.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) return categoryMatch && (stringMatch != nil) }) } 
+1
source share

All Articles