title: Add Search to tvOS UITableView
body: <p>I have a TableView with over 400 objects on my tvOS app, and it really needs to have search functionality on it.  </p>
  
  <p>After reading through some sample code from Apple, here is what I have.  SecondViewController is where the main TableView is, while I just kept the name of the resultsViewController the same as what Apple had it.  In the main one I have:</p>
  
  <pre><code>-(void)viewDidLoad {
      [super viewDidLoad];
      _resultsTableController = [[APLResultsTableController alloc] init];
      _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
      self.searchController.searchResultsUpdater = self;
      [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.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
  
  }
  - (void)viewWillAppear:(BOOL)animated {
      if (self.searchControllerWasActive) {
          self.searchController.active = self.searchControllerWasActive;
          _searchControllerWasActive = NO;
  
          if (self.searchControllerSearchFieldWasFirstResponder) {
              [self.searchController.searchBar becomeFirstResponder];
              _searchControllerSearchFieldWasFirstResponder = NO;
          }
      }
      NSBundle *bundle = [NSBundle mainBundle];
  
      self.files  = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"AIMPDF"];
      NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
  
      self.title = @"Devo Songs";
      self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
      NSLog(@"%@", filenames);
  
      if ([self savedSearchTerm])
      {
          //[[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
      }
      NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
      for (NSString *path in self.files) {
          [names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
      }
      self.files = names;
  
  
      self.tableView.backgroundColor = [UIColor whiteColor];
      self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"iphonebackground.png"]];
      [super viewDidLoad];
  
      UIBarButtonItem *plan = [[UIBarButtonItem alloc] initWithTitle:@"Plan Devo" style:UIBarButtonItemStylePlain target:self action:@selector(picking)];
      self.navigationItem.rightBarButtonItem = plan;
  
  
      [super viewWillAppear:animated];
  
  }
  - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
      [searchBar resignFirstResponder];
  }
  - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
      // update the filtered array based on the search text
      NSString *searchText = searchController.searchBar.text;
      NSMutableArray *searchResults = [self.files mutableCopy];
  
      // strip out all the leading and trailing spaces
      NSString *strippedString = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  
      // break up the search terms (separated by spaces)
      NSArray *searchItems = nil;
      if (strippedString.length &gt; 0) {
          searchItems = [strippedString componentsSeparatedByString:@" "];
      }
  
      // build all the "AND" expressions for each value in the searchString
      //
  
  
      // hand over the filtered results to our search results table
      APLResultsTableController *tableController = (APLResultsTableController *)self.searchController.searchResultsController;
      tableController.filteredProducts = searchResults;
      [tableController.tableView reloadData];
  }
  - (void)handleSearchForTerm:(NSString *)searchTerm
  {
      [self setSavedSearchTerm:searchTerm];
  
      if ([self searchResults] == nil)
      {
          NSMutableArray *array = [[NSMutableArray alloc] init];
          [self setSearchResults:array];
          array = nil;
      }
  
      [[self searchResults] removeAllObjects];
  
      if ([[self savedSearchTerm] length] != 0)
      {
          for (NSString *currentString in [self files])
          {
              if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
              {
                  [[self searchResults] addObject:currentString];
              }
          }
      }
  }
  </code></pre>
  
  <p>In the Results View Controller, I have:</p>
  
  <pre><code>@implementation APLResultsTableController
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return self.filteredProducts.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      NSString *filename = [[[self.filteredProducts objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];
      NSInteger row = [indexPath row];
      NSString *contentForThisRow = nil;
  
  
      contentForThisRow = filename;
  
      static NSString *CellIdentifier = @"CellIdentifier";
  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil)
      {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
  
  
      [[cell textLabel] setText:contentForThisRow];
      cell.textLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:90];
      cell.textLabel.textColor = [UIColor blackColor];
      cell.backgroundColor = [UIColor lightGrayColor];
      return cell;
  
  }
  
  
  @end
  </code></pre>
  
  <p>I get the Search Bar added at the top of my main TableView, but nothing changes when I start typing in letters.</p>