ios - Search bar filter isn't clearing the table when results don't match -
i have working search bar, , need clearing table when search text not match item in array (not including empty search bar).
i 1 of cells display message when no match found (like "no results available").
here code:
@iboutlet var searchfortool: uisearchbar! @iboutlet var tooltable: uitableview! var searchactive : bool = false { didset { if searchactive != oldvalue { tooltable?.reloaddata() } } } typealias item = (data: string, identity: string) var filtered: [item] = [] var items: [item] = [ (data: " data1", identity: "a"), (data: " data2", identity: "b") ] override func viewdidload() { super.viewdidload() self.navigationcontroller?.setnavigationbarhidden(true, animated: false) appstate.shared.category = "alphabetical" } @ibaction func backbutton(_ sender: any) { if let navcontroller = self.navigationcontroller { controller in navcontroller.viewcontrollers { if controller toolsviewcontroller { navcontroller.poptoviewcontroller(controller, animated: true) } } } } func searchbar(_ searchbar: uisearchbar, textdidchange searchtext: string) { filtered = items.filter { item in item.data.localizedcaseinsensitivecontains(searchtext) } searchactive = !filtered.isempty self.tooltable.reloaddata() } func numberofsections(in tableview: uitableview) -> int { return 1 } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { if(searchactive) { return filtered.count } return items.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! customcell cell.alphabeticallabel.layer.maskstobounds = true if(searchactive) { cell.alphabeticallabel.text = filtered[indexpath.row].data cell.alphabeticallabel.layer.cornerradius = 10 } else { cell.alphabeticallabel.text = items[indexpath.row].data cell.alphabeticallabel.layer.cornerradius = 10 } return cell } func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { let vcname: string if searchactive { vcname = filtered[indexpath.row].identity } else { vcname = items[indexpath.row].identity } let viewcontroller = storyboard?.instantiateviewcontroller(withidentifier: vcname) self.navigationcontroller?.pushviewcontroller(viewcontroller!, animated: true) } }
i know isn't best way of creating search bar functionality, i've been working while. i'm sure solution isn't complicated, i'm not having luck it.
any appreciated.
based on reply, add check see if search text empty or not:
func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { if(searchactive) { return filtered.count } else if (searchfortool.text? == "") { // check if "" or nil return items.count } else { return 0 // or 1 if want show cell "no found" text } }
you'll need adjust cellforrowatindexpath
similarly. , check wether when text property of search bar nil or empty string when user has not typed anything
Comments
Post a Comment