Alex Blair
Alex Blair

Reputation: 614

UISearchController with UITableView backed by Core Data iOS 8

Although there seems to be plenty of information on UISearchControllers, many of the approaches I have found have been deprecated in iOS 8. Most tutorials I have found use UISearchDisplayController, which is deprecated, and even the documentation on UISearchController uses convenience methods that have also been deprecated. I'm looking for someone who knows how to successfully search items in a UITableView that is backed by core data in iOS 8.

Currently I have a table view that that fetches and displays points of interest saved from a map view. Pretty standard boilerplate stuff - just persisting and fetching entities. The challenging part of this is adding a UISearchController, which recently replaced the UISearchDisplayController,to the table view that is backed by core data (the tableview displays any point of interest entity persisted to the data store). Using core data instead of a standard array is where the complication begins, and there isn't sufficient documentation using this particular approach. Open to any and all suggestions.

To kick things off, lets consider that I have stored my fetched items from core data in a mutable array. How would we go about using that to filter our table view?

self.searchResults = [NSMutableArray arrayWithCapacity:[[self.fetchController fetchedObjects] count]];

Upvotes: 1

Views: 537

Answers (1)

Mundi
Mundi

Reputation: 80271

You should be using a NSFetchedResultsController to display your Core Data backed data in a table view. Please read up on that first. Boilerplate available via Xcode templates.

For searching, you can e.g. just invalidate the fetched results controller and let it recreate it lazily. When creating it, add a predicate for the search term.

The UISearchController will simply help you keep track of the usual search event, i.e. search text change, scope changed, cancel etc.

Upvotes: 3

Related Questions