我有一个tableview,可以加载一个函数并在viewDidAppear中显示数据。当用户点击导航标题中的searchBar并搜索查询时,会使用相同的tableview(我假设)。
问题出在用户点击取消按钮后:调用原始数据的功能执行并打印正确的数据,但在tableView.reloadData之后不会出现在屏幕上。
我尝试将函数放在各个地方(didCancel,didEndEditing),函数调用/正确返回数据,但不会出现在表格中。
有任何建议或解决方法吗?
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() }
执行搜索的函数是updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) { query and places items in an array that's displayed by the original tableView }
我想知道是否有一个我不知道使用的tableView,但很明显searchController正在使用原始的tableView,因为它遵循我的可重用单元格的设计。
答案 0 :(得分:0)
您必须以这种方式填写您的UITableView:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.searchDisplayController!.active {
return self.filteredData.count
}
return self.defaultdData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
if self.searchDisplayController!.active {
cell.textLabel.text = self.filteredData[indexPath.row]
} else {
cell.textLabel.text = self.defaultData[indexPath.row]
}
return cell
}