Search the content of a UITableView in iphone?

Step 1: Create a Window base application using template . Give the application name “SearchTable”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Now we’ll add UITableViewController class and NSObject class to the project. Choose New file -> Select cocoa touch classes group and then selectUITableViewController and NSObject . Give the name of the class MainView and Product. Create a MainView.xib file in the resource folder.
Step 4: In the AppDelegate.h file  we have added UINavigationController in the file. So make the following changes in the file.
@interface SearchTableAppDelegate : NSObject  <UIApplicationDelegate> {
   UIWindow
 *window;
   UINavigationController      
 *navController;
}
   
@property (nonatomic, retain) IBOutlet UIWindow *window;
   
@property (nonatomic, retain) IBOutlet UINavigationController       *navController;
Step 5:  In the MainView.h file we make the following changes:
@interface MainView : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>  {
  NSArray                       *listContent;
 
 NSMutableArray        *filteredListContent;
 
 NSString              *savedSearchTerm;
  NSInteger             savedScopeButtonIndex;
 
 BOOL                  searchWasActive;
}
  @property (nonatomic, retain) NSArray *listContent;
 
 @property (nonatomic, retain) NSMutableArray *filteredListContent;
  @property (nonatomic, copy) NSString *savedSearchTerm;
 
 @property (nonatomic) NSInteger savedScopeButtonIndex;
 
 @property (nonatomic) BOOL searchWasActive;
Step 6: Double click your MainView.xib file open it to the Interface Builder. Drag Search Display from the library and place it to the main window. Drag search bar from the library and place it on the table view. Now select search display icon from the main window and bring up connection inspector and select searchBar to the SearchBar. Save it and go back to the Xcode.
Step 7: In the AppDelegate.m file make the following changes:
(void)applicationDidFinishLaunching:(UIApplication *)application
{
       
 NSArray *listContent = [[NSArray alloc] initWithObjects:
       
 [Product productWithType:@"Device" name:@"iPhone"],
       
 [Product productWithType:@"Device" name:@"iPod"],
       
 [Product productWithType:@"Device" name:@"iPod touch"],
       
 [Product productWithType:@"Desktop" name:@"iMac"],
       
 [Product productWithType:@"Desktop" name:@"Mac Pro"],
       
 [Product productWithType:@"Portable" name:@"iBook"],
       
 [Product productWithType:@"Portable" name:@"MacBook"],
       
 [Product productWithType:@"Portable" name:@"MacBook Pro"],
       
 [Product productWithType:@"Portable" name:@"PowerBook"], nil];
       
 
       
 
        MainView
 *mainViewController = [[MainView alloc]initWithNibName:@"MainView" bundle:nil];
        mainViewController.listContent
 = listContent;
       
 [listContent release];
       
 
        UINavigationController
 *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
        self.navController
 = navigationController;
       
 [navigationController release];
       
 [window addSubview:navController.view];
       
 [window makeKeyAndVisible];
}
Step 8: In the MainView.m file make the following changes:
(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
   
[self.filteredListContent removeAllObjects]; 
   
 for (Product *product in listContent)
 
 {
   
 if ([scope isEqualToString:@"All"] || [product.type isEqualToString:scope])
     
{
        NSComparisonResult result
 = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)range:NSMakeRange(0, [searchText length])];
       
 if (result == NSOrderedSame)
       
 {
         
[self.filteredListContent addObject:product];
         
}
     
 }
   
}
}
(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
 
 [self filterContentForSearchText:searchString scope:
   
[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
   
 
}
(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
   
 [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
   
 [[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:searchOption]];
     
}
Step 9: For the Product.m file make the following changes:

(id)productWithType:(NSString *)type name:(NSString *)name
{
  Product
 *newProduct = [[[self alloc] init] autorelease];
  newProduct.type
 = type;
  newProduct.name
 = name;
 
 return newProduct;
}
(void)dealloc
{
       
 [type release];
       
 [name release];
       
 [super dealloc];
}
Step 10: Compile and run the application on the simulator.


Popular posts from this blog

How to use nsxmlparser in Iphone SDk?

About Xcode 4 features

Blocks in iPhone?