Jack Nutkins
Jack Nutkins

Reputation: 1555

Adding a search bar to iPhone application that uses alphabetical sections?

I have an application that uses a UITableView which contains the names of products, these products are also split up into their respective sections based on their first letter.

Here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

        //Initialize alphabet array
        m_Alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K", @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"Other", nil];

        //Initialize alphabet distionary of arrays
        m_AlphabetDictionary = [[NSMutableArray alloc]init];

        //Populate distionary with a mutable array for each character
        //in the alphabet (plus one "Other" category)
        for (int i = 0; i < 27; i++)
            [m_AlphabetDictionary insertObject:[[NSMutableArray alloc] init] atIndex: i];

        // The number of products in the database
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];

        //For each product in the appDelgates products
        for (Product *product in appDelegate.m_Products){
            if ([product.category isEqualToString:productType]){

                [tempArray addObject:product];

                //firstLetter is equal to the first letter of the products name
                NSString * l_FirstLetter = [product.name substringToIndex:1];
                //convert firstString to uppercase
                l_FirstLetter = [l_FirstLetter uppercaseString];

                //The added flag ensures objects whose first letter isn't a letter
                //are added to array 26
                bool added = NO;

                for(int i=0; i<[m_Alphabet count]; i++){
                    //If the first letter of product name is equal to the letter at index i in theAlphabet
                    if ([l_FirstLetter isEqualToString:[m_Alphabet objectAtIndex:i]]) {
                        //Add product to section array for that letter
                        [[m_AlphabetDictionary objectAtIndex:i] addObject:product];

                        added = YES;
                    }

                }
                //If product hasn't been added to array, add it to "Others" category
                if(!added)
                    [[m_AlphabetDictionary objectAtIndex:26] addObject:product];

            }
        }

    //Set navigation controller title
    self.title = productType;

}

//Number of sections is equal to the length of the m_Alphabet array
//Letters A-Z plus "Other"
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
        return [m_Alphabet count]; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
        return [[m_AlphabetDictionary objectAtIndex:section] count]; 
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
    return m_Alphabet; 
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{
    return [m_Alphabet indexOfObject:title]; 
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    if ([[m_AlphabetDictionary objectAtIndex:section] count]==0) 
        return nil;

    return [m_Alphabet objectAtIndex:section]; 
}

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

    if([self.productType isEqualToString:@"All"]){
        Product *product = (Product *) [appDelegate.m_Products objectAtIndex:indexPath.row];

        cell.textLabel.text = product.name;

        // Configure the cell.
        return cell;
    }
    else {

            //Instead of using appDelegate.products use the new array that will be filled
            //by the numberOfReowsInSection method
            Product *product = (Product *)[[m_AlphabetDictionary objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

            cell.textLabel.text = product.name;
            // Configure the cell.
            return cell;
    }
}

What I'm looking to do is add a search bar to the top of my table view that behaves just like the search bar in the "All Contacts" section in the iPhones contacts application. I.E. When I search, all the sections disappear and just the search results are displayed until the search bar is blank again.

Can anyone point me in the right direction?

Thanks,

Jack

Upvotes: 0

Views: 1466

Answers (1)

CodaFi
CodaFi

Reputation: 43330

Looks like you need to uses UISearchDisplayController, which is most likely what the contacts application uses (along with mail, maps, safari, and music). It presents itself as a table view overlay that may or may not contain a scope bar (your choice) and filters results based on the search bar's text. A simple tutorial involving interface builder may be found here, and an apple example sans-IB may be found here.

Upvotes: 3

Related Questions