在TableView中显示搜索结果时没有结果 - swift

时间:2016-05-01 21:16:18

标签: ios swift uitableview uisearchbar uisearchdisplaycontroller

我正在尝试在UITableViewController中实现搜索,但不是基于过滤现有数组,而是调用我的API来搜索远程数据库中的值。

我认为我已正确实现了所有内容,因为我的textDidChangecellForRowAtIndexPath方法在适当的时间被调用,而我的数据数组包含了我需要的所有数据。但是 - 我的TableView中没有显示任何内容!它总是说“没有结果”。我找不到任何现有的解决方案,因为许多类在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...>)但是当我尝试解决此问题时,我的搜索栏完全停止了工作......

1 个答案:

答案 0 :(得分:4)

知道了。问题是:当我将搜索栏拖到Storyboard时,我选择了“搜索栏和搜索显示控制器”而不是“搜索栏”本身。在iOS8及更高版本中弃用的搜索显示控制器使代码无效。

因此,我不得不从文档大纲中删除搜索显示控制器,将插座重新连接到新的搜索栏并且它有效。希望它对某人有所帮助。