I have a tableview that loads a function and displays data in viewDidAppear. The same tableview (I assume) is used when a user taps on the searchBar in the navigation title, and searches for a query.
The problem is after the user taps the cancel button: the function for calling the original data performs and prints the correct data, but doesn't appear on the screen after tableView.reloadData.
I've tried placing the function in various places (didCancel, didEndEditing), and the function is called/correctly returns data, but doesn't appear on the table.
Any suggestions or workarounds?
class LocationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(animated: Bool) {
self.load0()
tableView.reloadData()
self.locationSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.delegate = self
controller.searchBar.searchBarStyle = .Minimal
controller.searchBar.sizeToFit()
self.navigationItem.titleView = controller.searchBar
return controller
})()
}
//below I try to remove the search data and reload the original data.
override func viewDidDisappear(animated: Bool) {
self.locationSearchController.active = false
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData()
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
self.category.removeAll(keepCapacity: false)
self.product.removeAll(keepCapacity: false)
self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData() }
The function that performs the search is updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) { query and places items in an array that's displayed by the original tableView }
I wondered if there was a tableView being used that I was not aware of, but it's clear that the searchController is using the original tableView because it follows the design for my reusable cell.
You must fill your UITableView this way: