Monday, 24 March 2014

Search using UISearchBar




Search as its name implies find something from a bunch of collection. Xcode offers us a search control to perform search on our list or whatever we want to let the user find the things listed in a real quick time without scrolling down to the bottom where that item is placed initially. In this post i am going to tell you that how we can use UISearchBar control in our iPhone app.

  First create a list of item you want to show in your table-view and add your table-view delegate methods to dispaly data in table-view. Now place a UISearchBar Control over your table-view and attach its delegate with File Owner, also create IBOutlet for search bar in your .h file, you can do this by select circle on right side of delegate property and dragging it toward file owner.Shown in  figure on left hand side of your screen.

Now its tiem to implement our UISearchBar delegate method, we will use below written four methods
-(void)searchBar:(UISearchBar*)searchBartextDidChange:(NSString*)text -(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar -(void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar  -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

All above methods are self explanatory as  per their name, we will perform our search  in first method searchBartextDidChange. Below is the code for performing serach

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
            NSMutableArray *tempArray =[[NSMutableArray alloc]initWithCapacity:0];
            for (NSDictionary *p in self.arrWineryData)
            {
                NSString *sTemp = [p valueForKey:@"zip"];
                NSRange titleResultsRange = [sTemp rangeOfString:text options:NSCaseInsensitiveSearch];
                
                if (titleResultsRange.length > 0)
                {
                    [tempArray addObject:sTemp];
                }
            }
            
            for (NSString *str in tempArray)
            {
                for (int i =0; i<[self.arrWineryData count]; i++)
                {
                    if ([str isEqualToString:[[self.arrWineryData objectAtIndex:i] valueForKey:@"zip"]])
                    {
                        [self.arrSearch addObject:[self.arrWineryData objectAtIndex:i]];
                    }
                }
            }
            NSLog(@"temp array searched == %@",self.arrSearch);
            if ([self.arrSearch count]>0)
            {
                [tbl_searchSates reloadData];
            }
        }
    }
}

In the above method we first created a temporary array i.e temp array to store zip codes matched with my original array.Then we rageOfString method of NSString class to check whether the text entered by user matched zip codes of our original array, if yes then add that zip code to temp array. Then we loop through our original array to get all matched zip codes and add it to search array and if search array count is greater then zero we will load out table.
NOTE: You can modify logic as per your need.


Second method will be called when we press cancel button of our search bar, in this we will hide keyboard and load original data by resign our searchbar.

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    SearchBar.text =@"";
    [searchBar resignFirstResponder];
    tbl_searchSates.hidden = YES;
    SearchBar.showsCancelButton = NO;
    [self.arrSearch  removeAllObjects];
}

Third method will be called when user start interacting with search bar by typing in it.Here we set showCancelButton property to yes
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
    aSearchBar.showsCancelButton = YES;
}

Forth method will be called when user press search button shown in keyboard, here we hide the keyboard by resign searchbar.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

No comments:
Write comments