[英]No Results when showing search results in TableView - swift
I am trying to implement search in UITableViewController
, but not based on filtering existing array, but rather calling my API to search values in remote database. 我试图在UITableViewController
实现搜索,但不是基于过滤现有数组,而是调用我的API来搜索远程数据库中的值。
I thought I implemented everything correctly, because my textDidChange
and cellForRowAtIndexPath
methods are called in proper time and my data array has all the data I need. 我以为我正确实现了所有事情,因为我的textDidChange
和cellForRowAtIndexPath
方法在适当的时间被调用,并且我的数据数组具有我需要的所有数据。 BUT - nothing shows up in my TableView! 但是-我的TableView中什么都没有显示! It always says "No Results". 它总是说“无结果”。 I can't find any existing solutions on SO since many classes were deprecated in iOS 8 and I'm struggling with implementing recent ones. 我找不到SO上的任何现有解决方案,因为iOS 8中不推荐使用许多类,而我正在努力实现最新的解决方案。 Any help appreciated! 任何帮助表示赞赏! My code: 我的代码:
class QuestionsController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var searchController = UISearchController()
var searchText: String?
var areSearchResultsExhausted: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: self)
self.searchBar.delegate = self
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
self.getQuestionsData(0, searchText: nil)
}
var questionsDataObjects = [Question]()
// MARK: - Get data from API
func getQuestionsData(startFromQuestionId: Int, searchText: String?) {
// Getting data from API stuff like:
let task = session.dataTaskWithRequest(req) { ... }
questionsDataObjects.append(...)
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questionsDataObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("QuestionCell", forIndexPath: indexPath) as! BasicQuestionCell
// Populate the table view cells
let questionCell = questionsDataObjects[indexPath.row]
cell.titleLabel.text = questionCell.content
// Load more results when user scrolled to the end
if (indexPath.row == questionsDataObjects.count-1
&& questionsDataObjects.count > indexPath.row
&& !areSearchResultsExhausted) {
print("qDO.count: \(questionsDataObjects.count) ; indexPath.row: \(indexPath.row)")
self.getQuestionsData(indexPath.row + 1, searchText: self.searchText)
}
return cell
}
// Handle searching
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
self.searchText = searchText
self.areSearchResultsExhausted = false
questionsDataObjects.removeAll()
self.getQuestionsData(0, searchText: searchText)
tableView.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchText = nil
self.areSearchResultsExhausted = false
questionsDataObjects.removeAll()
self.getQuestionsData(0, searchText: searchText)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
Two more important things: 还有两件事很重要:
1) updateSearchResultsForSearchController
never gets called! 1) updateSearchResultsForSearchController
永远不会被调用!
2) I have an error in console that says: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController...>)
but when I tried to solve this, my search bar stopped working COMPLETELY... 2)我在控制台中遇到一个错误,提示: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController...>)
但是当我尝试解决此问题时,搜索栏完全停止工作...
Got it. 得到它了。 The problem was: when I dragged the search bar into Storyboard, I selected "Search Bar and Search Display Controller" instead of "Search Bar" itself. 问题是:当我将搜索栏拖到情节提要中时,我选择了“搜索栏和搜索显示控制器”而不是“搜索栏”本身。 Search Display Controller, which is deprecated in iOS8 and later, made the code not work. 在iOS8和更高版本中已弃用的Search Display Controller使代码无法正常工作。
So, I had to delete the Search Display Controller from Document Outline, reconnect the outlets to new Search Bar and it worked. 因此,我不得不从“文档大纲”中删除“搜索显示控制器”,将插座重新连接到新的“搜索栏”,然后它才起作用。 Hope it helps somebody. 希望它能帮助到别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.