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 7517079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:20:01+00:00 2026-05-30T01:20:01+00:00

I Have a search bar that searches an array, and updates a UITableView with

  • 0

I Have a search bar that searches an array, and updates a UITableView with the results. The table view is a list of books, with titles and authors:

The titles and authors

Right now, the search bar only searches the titles but I would like to make it search the authors as well. Here is the search code I have (I got it from http://blog.webscale.co.in/?p=228).

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [tableData removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""]||searchText==nil){
        [tableView reloadData];
        return;
    }

    for(NSString *name in dataSource){
         NSInteger counter = 0;

        //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
        if(r.location != NSNotFound)
            [tableData addObject:name];


            counter++;
    }
        //[pool release];


    [tableView reloadData];

}

dataSource is the NSMutable Array that contains the titles. the array that contains the authors is called “author”. “tableData” is the array that stores the cells that should appear on the screen (the cells that contain terms being searched for).

Thanks so much,

Luke

  • 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-30T01:20:02+00:00Added an answer on May 30, 2026 at 1:20 am

    I would modify the dataSource array to contain both the titles and authors by creating an NSDictionary with key value pairs (a Book class would be better).

    //Do this for each book
    NSDictionary * book = NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
        title, @"TITLE", author, @"AUTHOR", nil];
    [dataSource addObject:book];
    

    After that you can change your search method to work with the NSDictionary instead.

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
    
        [tableData removeAllObjects];
    
        if(searchText != nil && ![searchText isEqualToString:@""]){
    
            for(NSDictionary * book in dataSource){
                NSString * title = [book objectForKey:@"TITLE"];
                NSString * author = [book objectForKey:@"AUTHOR"];
    
                NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
                NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];
    
                if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
                    [tableData addObject:book];
                }
    
        }
    
        [tableView reloadData];
    }
    

    Note using this method you would have the change your cellForRowAtIndexPath method to work with the NSDictionary and not the title strings.

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

Sidebar

Related Questions

I have a search bar in my web site that searches for all the
So I have this website that has a search feature which searches a table
I have set up a search bar for my table view, and I would
I have the following method for my search bar, that searches for the key
I have a UISearchBar that searches and displays results successfully. If I press the
In OS3.0 a lot of plain table views have an initially hidden search bar
I would like to have a search box integrated into a nav bar that
I have a jqGrid that is using the toolbar search with some text searches
I have created a search bar on GAE similar to facebook that shows you
I have a search website that needs to have the search bar and logo

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.