【问题标题】:No Results when showing search results in TableView - swift在 TableView 中显示搜索结果时没有结果 - swift
【发布时间】:2016-05-01 21:16:17
【问题描述】:

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

我认为我正确地实现了一切,因为我的 textDidChangecellForRowAtIndexPath 方法在适当的时间被调用,并且我的数据数组包含了我需要的所有数据。但是 - 我的 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


    【解决方案1】:

    知道了。问题是:当我将搜索栏拖到 Storyboard 中时,我选择了“搜索栏和搜索显示控制器”而不是“搜索栏”本身。在 iOS8 及更高版本中已弃用的 Search Display Controller 导致代码无法正常工作。

    所以,我不得不从 Document Outline 中删除 Search Display Controller,将插座重新连接到新的 Search Bar,然后它就可以工作了。希望它可以帮助某人。

    【讨论】:

    • @MaxvonHippel 您只能在发布 2 天后接受您自己的答案
    • 哈哈,哦,好吧,对不起,我忘了,那是代表的事情。谢谢提醒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多