0

I have created a Table View Controller that I am happy with, and now want to add a search view to it. I have added the 'Search bar and search display controller' using the storyboard, and tried to follow many tutorials for adding a search bar, but it seems that all of them are now deprecated. I do not want to add the search programatically, as I want it to be in quite a specific position on the storyboard, not at the top of the screen.

The issue is that the updateSearchResultsForSearchController is not being called at all. I've put a break in this function to confirm this.

I have the following code:

class ViewItemTableViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate {

// lots of other unrelated things

@IBOutlet weak var searchBar: UISearchBar!

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {       
        listItems = allItems.retrieveItemById(1)
        self.tableView.reloadData()
    }

}

How do I correctly configure the search bar so that this function is called on change of the search value?

Christie
  • 25
  • 5
  • refer this link http://stackoverflow.com/questions/29664315/how-to-implement-uisearchcontroller-in-uitableview-swift – Rohit Pradhan Jul 01 '16 at 08:43
  • Hi, that link is to create the search programatically, which I don't want to do as I need it in a specific position on the storyboard. Unless there's a way I can position it programatically? – Christie Jul 01 '16 at 08:55
  • Your tableview is already present in storyboard. When you execute "self.tableView.tableHeaderView = searchController.searchBar", the search bar is added at the header of tableview that you have defined in your story board. So you can still work with your story board and just add the searchController through that explanation. – chejaras Jul 01 '16 at 09:03
  • Unfortunately this line "self.tableView.tableHeaderView = searchController.searchBar" deletes my existing header, and replaces it with the search bar at the top, which is what I'm trying to avoid – Christie Jul 01 '16 at 09:17

1 Answers1

0

I think you could just use a UISearchBar without a search display controller.

Ensure that your UISearchBar delegate is set from storyboard to ViewItemTableViewController. Then you can use the following delegate func to perform your task:

func searchBarSearchButtonClicked(searchBar: UISearchBar)

func searchBarTextDidEndEditing(searchBar: UISearchBar)

Zac Kwan
  • 5,587
  • 4
  • 20
  • 27