ios - Search Bar in a table view -
i have tableview application searchbar showing array list of groceries. after search made user click on grocery , send second tableview. working fine, problem is:
in next (second) table view information grocery searchbar still there , keyboard well.
how can dismiss keyboard , searchbar should not appear in second table view. more welcome :)
// dictionarytableviewcontroller.swift import uikit class dictionarytableviewcontroller: uitableviewcontroller, uisearchresultsupdating { var searchcontroller:uisearchcontroller! var searchresults:[dictionary] = [] var dictionaries:[dictionary] =[ dictionary (word: "grocery1", definition: "this grocery1 definition"), dictionary (word: "grocery2", definition: "this grocery2 definition"), dictionary (word: "grocery3", definition: "this grocery3 definition"), dictionary (word: "grocery4", definition: "this grocery4 definition"), ] override func viewdidload() { super.viewdidload() searchcontroller = uisearchcontroller(searchresultscontroller: nil) tableview.tableheaderview = searchcontroller.searchbar searchcontroller.searchresultsupdater = self searchcontroller.dimsbackgroundduringpresentation = false } override func didreceivememorywarning() { super.didreceivememorywarning() } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { if searchcontroller.active { return searchresults.count } else { return dictionaries.count } } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cellidentifier = "cell" let cell = tableview.dequeuereusablecellwithidentifier(cellidentifier, forindexpath: indexpath) as! dictionarytableviewcell let dictionary = (searchcontroller.active) ? searchresults[indexpath.row]: dictionaries[indexpath.row] // configure cell... cell.wordlabel.text = dictionary.word return cell } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showdictionarydetail" { if let indexpath = tableview.indexpathforselectedrow { let destinationcontroller = segue.destinationviewcontroller as! dictionarydetailviewcontroller destinationcontroller.dictionary = (searchcontroller.active) ? searchresults[indexpath.row] : dictionaries[indexpath.row] } } } func updatesearchresultsforsearchcontroller(searchcontroller: uisearchcontroller) { if let searchtext = searchcontroller.searchbar.text { filtercontentforsearchtext(searchtext) tableview.reloaddata() } } func filtercontentforsearchtext(searchtext: string) { searchresults = dictionaries.filter({ (dictionary:dictionary) -> bool in let wordmatch = dictionary.word.rangeofstring(searchtext, options: nsstringcompareoptions.caseinsensitivesearch) return wordmatch != nil })}}
when using searchcontroller, can deactivate when leaving screen. insert self.searchcontroller.active = false
you can @ viewwilldisappear
or prepareforsegue
. this
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showdictionarydetail" { if let indexpath = tableview.indexpathforselectedrow { let destinationcontroller = segue.destinationviewcontroller as! dictionarydetailviewcontroller destinationcontroller.dictionary = (searchcontroller.active) ? searchresults[indexpath.row] : dictionaries[indexpath.row] self.searchcontroller.active = false } } }
Comments
Post a Comment