Jerry Chen
Jerry Chen

Reputation: 31

UISearchController not updating

I am developing using xcode 6.2 in ios8 and swift, I am trying to implement a UISearchController inside a UITableViewController which could be updated after the search, I wrote some simple code just to check if it works, but when I compile, the search bar is there, but when I enter the text and hit search, nothing happens:

here is my code:

import UIKit

class searchresult: UITableViewController,UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating {

    var result:[String] = ["a","b","c"]

    override func viewDidLoad() {
        super.viewDidLoad()

        var searchcon = UISearchController(searchResultsController: self)
        searchcon.delegate = self
        searchcon.searchBar.delegate = self
        searchcon.searchBar.sizeToFit()
        self.tableView.tableHeaderView = searchcon.searchBar
        searchcon.searchResultsUpdater = self
        searchcon.searchBar.showsSearchResultsButton = true
        self.definesPresentationContext = true
        searchcon.dimsBackgroundDuringPresentation = false

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
        cell.textLabel!.text = result[indexPath.row]
        return cell
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return result.count
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    func updateSearchResultsForSearchController(searchController: UISearchController) {

        println("updating")
        result = ["1"]
        self.tableView.reloadData()   
    }
}

Upvotes: 2

Views: 1693

Answers (3)

Pavel Vergeev
Pavel Vergeev

Reputation: 3370

As Praveen Gowda IV said, you wanna change the searchResultsController from self to nil. Also you need to declare your searchcon outside viewDidLoad:

var searchcon = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    // your implementation
}

However, the behaviour you get might be not what you want because updateSearchResultsForSearchController gets called before the search:

Called when the search bar becomes the first responder or when the user makes changes inside the search bar. (Apple)

Upvotes: 2

Praveen Gowda I V
Praveen Gowda I V

Reputation: 9637

As I suggested in my comment, you need to change self to null in the initialization of UISearchController.

override func viewDidLoad() {

    super.viewDidLoad()

    var searchcon = UISearchController(searchResultsController: nil)
    searchcon.delegate = self
    searchcon.searchBar.delegate = self
    searchcon.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchcon.searchBar
    searchcon.searchResultsUpdater = self
    searchcon.searchBar.showsSearchResultsButton = true
    self.definesPresentationContext = true
    searchcon.dimsBackgroundDuringPresentation = false

}

What user3694363 said about updateSearchResultsForSearchController getting called even before text is changed, is correct. But you can follow the below trick to prevent doing anything when it becomes the first responder

func updateSearchResultsForSearchController(searchController: UISearchController) {

        // No need to update anything if we're being dismissed.
        if !searchController.active {
            return
        }

        // do the filtering here
    }

Upvotes: 0

Gajendra Mali
Gajendra Mali

Reputation: 306

Try this code With other stuff in as it is.

class searchresult: UITableViewController {      

var searchcon = UISearchController(searchResultsController: nil)

searchcon.searchResultsUpdater = self
searchcon.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchcon.searchBar
searchcon.searchBar.showsSearchResultsButton = true
self.definesPresentationContext = true
searchcon.dimsBackgroundDuringPresentation = false
}

Upvotes: -1

Related Questions