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

  • Home
  • SEARCH
  • 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 6892269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:30:50+00:00 2026-05-27T06:30:50+00:00

I have set up a search bar for my table view, and I would

  • 0

I have set up a search bar for my table view, and I would like to have it search through both the textLabels (main text), and detailTextLabels (underlying text), while maintaining the relationship between the main labels and detail labels when narrowing down the search. Right now, I can search through the main labels only, but the detail labels will not stay with the related main labels. I have two arrays that carry the strings for the main labels and the detail labels. Here is some code.

The initialization of the arrays carrying the main labels (groceryStores) and the detail labels (groceryStoreAddresses), as well as the search array

 - (void)viewDidLoad
{
    [super viewDidLoad];

    self.groceryStores = [NSArray arrayWithObjects:@"Safeway",@"Grocery Store 1",@"Grocery Store 2",@"Grocery Store 3",@"Grocery Store 4",@"Grocery Store 5",@"Grocery Store 6",@"Grocery Store 7",nil];

    self.groceryStoreAddresses = [NSArray arrayWithObjects:@"1444 Shattuck Place, Berkeley, CA   94709",@"Address 1",@"Address 2",@"Address 3",@"Address 4",@"Address 5",@"Address 6",@"Address 7",nil];

    self.searchGroceryStores = [NSMutableArray arrayWithCapacity:[self.groceryStores count]];


}

The search method

 - (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self.searchGroceryStores removeAllObjects] ;

    for (NSString *groceryStore in self.groceryStores) {



        NSRange result = [groceryStore rangeOfString:searchString options:
                          NSCaseInsensitiveSearch];
        if (result.location != NSNotFound)
            [self.searchGroceryStores addObject:groceryStore];

    }

    return YES;
}

Rows in section tweaked for search functionality

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchGroceryStores count];
    }
    else {

    return [self.groceryStores count];
    }
}

cellForRowAtIndexPath tweaked for search functionality

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCellAccessoryDisclosureIndicator"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchGroceryStores objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
    }
    else {
        cell.textLabel.text = [self.groceryStores objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
    }

    // Configure the cell...

    return cell;
}

Please help if you can. Thank you so much for your time, I appreciate it.

  • 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-27T06:30:50+00:00Added an answer on May 27, 2026 at 6:30 am

    You need to keep a searchGroceryStoreAddresses array in parallel with your searchGroceryStores array. Example:

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
        [self.searchGroceryStores removeAllObjects];
        [self.searchGroceryStoreAddresses removeAllObjects];
        for (int i = 0, c = self.groceryStores.count; i < c; ++i) {
            NSString *groceryStore = [self.groceryStores objectAtIndex:i];
            NSString *address = [self.groceryStoreAddresses objectAtIndex:i];
            NSRange result = [groceryStore rangeOfString:searchString options:
                              NSCaseInsensitiveSearch];
            if (result.location != NSNotFound) {
                [self.searchGroceryStores addObject:groceryStore];
                [self.searchGroceryStoreAddresses addObject:address];
            }
        }
        return YES;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up the full-text search functionality in SQL Server 2008 express. This
I have some progress bar (search results), which value is dynamically set on document.ready
How can i set a default text in the search bar that looks grey
I have a search bar which is at the bottom of the view. How
I have :set hlsearch as default value. When I search for something, search terms
I have a large (600 odd) set of search and replace terms that I
I have a site collection and I want to set the search center value
I have implemented the search bar in my app and it´s working fine, but
I've got a MySQL table that acts like a nested set in order to
I am trying to add a UISearchbar to table header view. I have the

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.