Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:59:32+00:00 2026-05-21T17:59:32+00:00

I have spent hours reading a couple dozen different blogs and others q’s here

  • 0

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.

My question is how do you make this thing work. I’ve tried it in IB and through code.

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?)

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.

 - (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 :).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:59:32+00:00Added an answer on May 21, 2026 at 5:59 pm

    A couple possible points of failure I see:

    1) are you setting your viewcontroller as a delegate in its .h file? example:

    @interface myViewController : UIViewController <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>{
    

    2) I can’t tell if that is your complete code, or just relevant snippets, but if it’s the full code there are a couple holes before you will see any data showing up. You’d need to add a tableview somewhere in there to display the results into and fill out the second if statement in your cellForRowAtIndexPath method.

    I’d recommend checking out the sample code for TableSearch from Apple, that ought to get you moving in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent quite a few hours reading and learning about LINQ to XML , but
I'm sorry to ask this question but I have spent hours trying to understand
I've spent hours reading up about AppDomains, but I'm not sure they work quite
This might be quite simple but I have spent hours in Google but couldn't
I have spent hours on this and read everybit on the web about memory
I have spent hours with this issue. I'm about ready to tear my project
Firstly apologies if this is really simple but I have spent hours trying and
I have spent hours in this problem and my fellows couldn't help me out.
I hope this is a relatively easy problem although I have spent hours websearching
Have spent an hour trying to solve this - but to no avail. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.