My Scenario, I am loading JSON
data into tableview
here I am maintaining two segment controller button for single tableview
with search-bar
. First segment button click to search I can get search result well and if I click segment button two there is also showing same search result. So, when I click segment one to two I need to clear search result and load normal data. Same scenario working well when I click close button within searchBar
.
My Code
@IBAction func switchTableviewAction(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
print(“one”)
self.searchResultClear()
currentTableView = sender.selectedSegmentIndex
self.tableView.reloadData()
case 1:
print(“two”)
self.searchResultClear()
currentTableView = sender.selectedSegmentIndex
self.tableView.reloadData()
default:
break;
}
}
// MARK: Search Result Clear working but not clearing result
func searchResultClear() {
//self.searchBar.text = ""
//self.searchBar.showsCancelButton = false
//self.filteredLanguages.removeAll()
//self.tableView.reloadData()
}
Along with clearing search bar, you also need to remove the filtering from data source. By data source, I mean the array of objects you are showing in table view. You must be using a filter function with the filter text. If you want to clear the search bar, you also need to restore the original JSON data (the unfiltered one) and then call reloadData
You just need to call the searchBar textDidChange
method with empty text when segment control selection is changed
@IBAction func switchTableviewAction(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
self.searchBar(self.searchBar, textDidChange: "")
case 1:
self.searchBar(self.searchBar, textDidChange: "")
default:
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.