ios - Return Checkmark after using UISearchBar - Swift -
i've created uisearchbar app using tutorial. works fine, cells configured correctly, , able search username.
now trying add (checkmark ✔︎) each cell, allowing me (select ✔︎) multiple users in list.
the functionality works fine, when search list, (select ✔︎) user, , return main tableview, user not remain selected , vice-versa.
how can (checkmark ✔︎) multiple users , maintain checkmark before or after use uisearchbar?
class inviteviewcontroller: uitableviewcontroller, uitableviewdatasource, uitableviewdelegate, uisearchbardelegate, uisearchdisplaydelegate { var allfriends = [friend]() var filteredfriends = [friend]() override func viewdidload() { super.viewdidload() ###call friends getfriends() } override func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { if tableview == self.searchdisplaycontroller!.searchresultstableview { return self.filteredfriends.count } else { return self.allfriends.count } } var selectedfriendindex:int? = nil override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = self.tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell var friend : friend if tableview == self.searchdisplaycontroller!.searchresultstableview { friend = filteredfriends[indexpath.row] } else { friend = allfriends[indexpath.row] } ###configure cell cell.textlabel!.text = friend.username cell.accessorytype = uitableviewcellaccessorytype.disclosureindicator if (indexpath.row == selectedfriendindex) { cell.accessorytype = uitableviewcellaccessorytype.checkmark } else { cell.accessorytype = uitableviewcellaccessorytype.none } return cell } override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { tableview.deselectrowatindexpath(indexpath, animated: true) selectedfriendindex = indexpath.row let cell = tableview.cellforrowatindexpath(indexpath) if let index = selectedfriendindex { if (cell?.accessorytype == .checkmark) { cell!.accessorytype = .none } else { cell!.accessorytype = .checkmark } } } func filtercontentforsearchtext(searchtext: string, scope: string = "all") { self.filteredfriends = self.allfriends.filter({( friend : friend) -> bool in var usernamematch = (scope == "all") || (friend.username == scope) var stringmatch = friend.username.lowercasestring.rangeofstring(searchtext.lowercasestring) return usernamematch && (stringmatch != nil) }) } func searchdisplaycontroller(controller: uisearchdisplaycontroller, shouldreloadtableforsearchstring searchstring: string!) -> bool { self.filtercontentforsearchtext(searchstring) return true } func searchdisplaycontroller(controller: uisearchdisplaycontroller, shouldreloadtableforsearchscope searchoption: int) -> bool { self.filtercontentforsearchtext(self.searchdisplaycontroller!.searchbar.text) return true } func searchdisplaycontroller(controller: uisearchdisplaycontroller, willhidesearchresultstableview tableview: uitableview) { self.tableview.reloaddata() } }
you need keep track of friends selected in (an array in) model.
you add bool each friend, indicating whether selected.
this preserve selection information when search , return.
Comments
Post a Comment