为什么在使用 UISearchResultsUpdating 取消按钮后我的 tableview 是空白的?
Why is my tableview blank after using UISearchResultsUpdating cancel button?
我有一个 table 视图,它加载一个函数并在 viewDidAppear 中显示数据。当用户点击导航标题中的搜索栏并搜索查询时,将使用相同的 table 视图(我假设)。
问题出在用户点击取消按钮后:调用原始数据的函数执行并打印了正确的数据,但在tableView.reloadData之后没有出现在屏幕上。
我试过把函数放在不同的地方(didCancel,didEndEditing),函数是called/correctly returns数据,但没有出现在table上。
有什么建议或解决方法吗?
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,因为它遵循我的可重用设计细胞.
您必须以这种方式填充您的 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
}
我有一个 table 视图,它加载一个函数并在 viewDidAppear 中显示数据。当用户点击导航标题中的搜索栏并搜索查询时,将使用相同的 table 视图(我假设)。
问题出在用户点击取消按钮后:调用原始数据的函数执行并打印了正确的数据,但在tableView.reloadData之后没有出现在屏幕上。
我试过把函数放在不同的地方(didCancel,didEndEditing),函数是called/correctly returns数据,但没有出现在table上。
有什么建议或解决方法吗?
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,因为它遵循我的可重用设计细胞.
您必须以这种方式填充您的 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
}