【发布时间】:2016-05-01 21:16:17
【问题描述】:
我正在尝试在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...>) 但是当我尝试解决这个问题时,我的搜索栏完全停止工作...
【问题讨论】:
标签: ios swift uitableview uisearchbar uisearchdisplaycontroller