ios - How to use scope bar with UISearchController -
i able create scope bar uisearchcontroller & set titles
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 sort using scope bar. current sorting method follows
func updatesearchresultsforsearchcontroller(searchcontroller: uisearchcontroller) { filteredtabledata.removeall(keepcapacity: false) let searchpredicate = nspredicate(format: "self contains[c] %@", searchcontroller.searchbar.text) let array = (tabledata nsarray).filteredarrayusingpredicate(searchpredicate) filteredtabledata = array as! [string] tableview.reloaddata() }
even though late, might someone! suppose have object person , array of filteredpeople keep people you've filtered, then
struct person { let name:string } func updatesearchresultsforsearchcontroller(searchcontroller:uisearchcontroller) { //start filtering when user starts write 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 case searching all, , search case insensitive let categorymatch = (scope == "all") let stringmatch = person.name.rangeofstring(searchtext, options: nsstringcompareoptions.caseinsensitivesearch) return categorymatch && (stringmatch != nil) }) }
Comments
Post a Comment