I am a newbie to Swift and I am trying to add search functionality to my UITableView
which is in a UIViewController
class. I googled a lot and found that UISearchDisplayController
has been deprecated and replaced by UISearchController. When I tried to search for tutorials I did not find any specifically for UITableView
. Some of them were for implementing in UITableViewController
. So here are my questions:
Can we implement
UISearchController
in aUITableView
that is in aUIViewController
class? (I guess we can)If we can, please convert these line of codes written in Objective-C. Or if there are some really good tutorials please let me know.
@property (strong, nonatomic) UISearchController *searchController; UITableViewController *searchResultsController = [[UITableViewController alloc] init]; // Init UISearchController with the search results controller self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; // Link the search controller self.searchController.searchResultsUpdater = self;
EDIT : I am trying to assign the search results updater in as
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
self.addTableViewWithSection()
self.searchResultsController = UITableViewController()
var controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
}
}
*However on the line controller.searchResultsUpdater = self i get the eroor as Cannot assign a value of typ "ViewController" to a value of type "UISearchResultsUpdating?" .. So i really doubt if i can use UISearchController in UIViewController type class.
For those looking for a bit more context on all the aspects of implementing this, here's a tutorial on how to implement UISearchController
In addition to the above answer by Andy Ibanez, you also need to add code to your UITableViewDataSource methods to show the newly filtered results in the tableview.
Also, don't forget to call
tableView.reloadData()
in your updateSearchResults method - the UISearchController does not automatically tell the tableView to reload its data.Yes, the way search works has been radically changed for what I consider to be a better system. The new system is much better and straight to the point for most simple implementations. Using it is pretty straightforward.
First, make your class comply with the
UISearchResultsUpdating
protocol.class MyTableViewController: UITableViewController, UISearchResultsUpdating {}
Add it the search controller property:
Add the search bar in viewDidLoad:
And finally, implement the
updateSearchResults:for
method that comes from theUISearchResultsUpdating
protocol:Your implementation of this method will of course depend on where you are searching the data from. It will update your current table view with the contents of your search as you type them. Make sure you update your data source and reload the table view in the
updateSearchResultsForSearchController
method. Again, it updates as you type so make sure that if your data to search in is big or if you are searching from the internet, add some concurrency in this method.If you want to use a different controller to populate your search results with, you can pass that VC when initialising the
searchController
property.EDIT: I have reread your question and it looks like I forgot to add something important.
UITableViewController has a member called
tableView
. You can grab the controller's table view , but to populate yourUITableView
, you don't need to touch it. You can't use the SearchController logic directly with UITableView. You gotta work with the controller to get it there. Use the UITableViewController delegate and data source methods to get your table UI updated with your search results.Please read on using UITableViewController, UITableViewDelegate, and UITableViewDataSource so you can understand how to get your search results there.
In case of anyone, who is freaking around like me..this code might help