Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Could somebody please help me understand this? obj-c, ios

Could somebody please help me understand this answer I got on StackOverFlow? The link to the question is here but you can also view the answer I need help understanding below: http://stackoverflow.com/questions/29369357/searching-a-uitableview-not-a-uitableviewcontroller-with-uisearchcontroller-and#comment46937825_29369754 .

The Apple sample project only applies to UITableViewControllers I think and I have a UITableView inside a ViewController. Would this still work? Furthermore, why does Apple's sample project have so many classes? Whereas, I only have one which is the CustomMessagesViewController which handles the table view I want to filter and the saving data to Parse.com stuff.

If you're just looking for some code and an explanation to go off of, I'd recommend looking at Apple's sample project here:

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

The main things to note are that you can't do this from a storyboard. Instead, you will create a property of type UISearchController on the view controller where your search currently lives. Then, you'll create a new class that is either a subclass of UITableViewController or has a UITableView on it, and you will set that as the searchResultsController of the your SearchController property.

Another important thing to note is that unlike in the past with UISearchDisplayController, UISearchController is a whole different view controller that displays your search results on top of your main table view. (This is achieved by the definesPresentationContext variable in the code).

While Apple provides you everything you need in the link above, here is the most important part of the code with some comments:

- (void)viewDidLoad {
    [super viewDidLoad];
    // results table controller is the same as what your search display controller used to be
    _resultsTableController = [[APLResultsTableController alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
    // this says 'tell this view controller when search updates are available'
    self.searchController.searchResultsUpdater = self;
    // this places the search bar atop the table view since it's hard to do this via storyboard
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
    self.resultsTableController.tableView.delegate = self;
    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
    self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

    // Search is now just presenting a view controller. As such, normal view controller
    // presentation semantics apply. Namely that presentation will walk up the view controller
    // hierarchy until it finds the root view controller or one that defines a presentation context.
    //
    self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed
}

Any help is appreciated. Thanks, Arman