Search The Content Of A UITableView In iPhone

In this application we will see how to search the contents of a UITableView using UISearchBar and UISearchDisplayController, effectively filtering in and out the contents of that table.

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.

 

 

 

Step 5:  In the MainView.h file we make the following changes:

 

 

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:

 

 

 

Step 8: In the MainView.m file make the following changes:

 

 

 

Step 9: For the Product.m file make the following changes:

 

 

 

Step 10: Compile and run the application on the simulator.

Leave a Comment:

10 comments
Viki says March 26, 2010

Hi

Nice tutorial….thanks for it. I am new to iphone programming.

I have a navigation controller.When i select one of the row it goes to another viewController in which i have the above code. Now my problem is that its not a rootViewcontroller. In my rootviewcontroller i have some other data. so how will i modify this accordingly. Please help

– (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];
}

Thanks
Viki

Reply
Sushant says March 27, 2010

See the below link, i think it will be helpful to you..!!!

Link: http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-implement-tableview-with-navigationcontroller-in-iphone/

Reply
sapana says June 3, 2010

Nice tutorial…..

Reply
ruben says January 11, 2011

Hello thanks for the tutorial, but I would like to do that when I select a cell loads a Web instead of to put text and if I select another different cell loads another different Web, I have few knowledge I have been two months trying to learn and I would like if you could give a version me as I say to you, thanks I wait for answer

Reply
Shay says February 10, 2011

I have a dataSource for my TableView that is an NSMutableArray

how do I search the NSMutableArray?

Do you have a way to download the sample code?

Reply
wyvernbai says March 22, 2011

Hi

I am new to iphone programming.
I have searched for a tutorial many times. I think this is very helpful for me. Can i get your sample code? thanks very much.

this is my email:[email protected]

Reply
IORI says May 9, 2011

Thank you very much!The text is helpful for me.

Reply
Kubilay says September 29, 2011

Hi,
I have a problem in last step (Step 9).

I write the code into Product.m but it gives error in this lines:
newProduct.type = type; //error: Property ‘type’ not found on object of type ‘Product *’
newProduct.name = name; //error: Property ‘name’ not found on object of type ‘Product *’

I guess I’m having an issue about inheritance from NSObject but I just couldn’t solve. Hope you can help me with this.

Reply
krunal says November 19, 2011

i want this sample code to work on iPad can you plz provide me sir…
my id is.
[email protected]
or [email protected]
it’s urgent..

Reply
akshay says September 15, 2012

need download link for this source code.

Reply
Add Your Reply