简体   繁体   中英

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.

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. BUT - nothing shows up in my 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. 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!

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...

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM