[英]No Results when showing search results in TableView - swift
我試圖在UITableViewController
實現搜索,但不是基於過濾現有數組,而是調用我的API來搜索遠程數據庫中的值。
我以為我正確實現了所有事情,因為我的textDidChange
和cellForRowAtIndexPath
方法在適當的時間被調用,並且我的數據數組具有我需要的所有數據。 但是-我的TableView中什么都沒有顯示! 它總是說“無結果”。 我找不到SO上的任何現有解決方案,因為iOS 8中不推薦使用許多類,而我正在努力實現最新的解決方案。 任何幫助表示贊賞! 我的代碼:
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) {
}
還有兩件事很重要:
1) updateSearchResultsForSearchController
永遠不會被調用!
2)我在控制台中遇到一個錯誤,提示: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController...>)
但是當我嘗試解決此問題時,搜索欄完全停止工作...
得到它了。 問題是:當我將搜索欄拖到情節提要中時,我選擇了“搜索欄和搜索顯示控制器”而不是“搜索欄”本身。 在iOS8和更高版本中已棄用的Search Display Controller使代碼無法正常工作。
因此,我不得不從“文檔大綱”中刪除“搜索顯示控制器”,將插座重新連接到新的“搜索欄”,然后它才起作用。 希望它能幫助到別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.