title: UISearchDisplayController - How to implement and get it working
body: <p>I have spent hours reading a couple dozen different blogs and others q's here on SO about this, but I'm at an impasse. </p>
  
  <p>My question is how do you make this thing work. I've tried it in IB and through code. </p>
  
  <p>When I tried it in IB the search bar would never show even though I dragged it onto the table view (of a UITableViewController) and it "snapped" onto the table where the header belongs. But the search bar would never show up. (That's a sub-question, anyone know why?)</p>
  
  <p>So I tried setting up just in code. I get it allocated, assign the delegate, and data source, but I never get that table view that says "No results". The bar showed up and I can type into it, but it always just shows an empty table even though I know there should be results. I am suspicious its not showing the right table since the cell w/ the "No results" text doesn't show.</p>
  
  <pre><code> - (void)viewDidLoad {
  
      [super viewDidLoad];
  
      names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil];
      results = [[NSMutableArray alloc] init];
  
      UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
      sb.tintColor = [UIColor greenColor];
      searchTable.tableHeaderView = sb;
  
      UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
      [self setSearchDisplayController:sdc];
      [sdc setDelegate:self];
      [sdc setSearchResultsDataSource:self];}
  
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if (tableView == self.searchTable) {
          // normal table view population
          return [names count];
      }
      if(tableView == self.searchDisplayController.searchResultsTableView){
          // search view population
          return [results count];
      }
      return 0;
  }
  
  // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  {    
      static NSString *CellIdentifier = @"Cell";
  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          cell.selectionStyle = UITableViewCellSelectionStyleGray;
      }
  
      if (tableView == self.searchTable) {
          // normal table view population
          cell.textLabel.text = [names objectAtIndex:indexPath.row];
      }
      if(tableView == self.searchDisplayController.searchResultsTableView){
          // search view population
      }
  
      cell.textLabel.textColor = [UIColor blueColor];
  
      return cell;
  }
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
  {   
      if (tableView == self.searchTable) {
          // normal table view population
      }
      if(tableView == self.searchDisplayController.searchResultsTableView){
          // search view population
      }   
  }
  
  
  - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  {
      // Return YES to cause the search result table view to be reloaded.
      return YES;
  }
  
  - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
  {
      [results removeAllObjects]; // First clear the filtered array.
  
      /*
       Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
       */
      for (NSString *str in names)
      {
          NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])];
          if (result == NSOrderedSame)
          {
              [results addObject:str];
          }
      }
  }
  
  Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :).
  </code></pre>