sinh99
sinh99

Reputation: 3979

Create search field in Tableview

I am creating an application with a table view, which has a large amount of data in it. Because of this, it is necessary for me to use a search field.

Does anyone have any idea how to create a search-option in a tableview?

Upvotes: 5

Views: 1364

Answers (1)

Joetjah
Joetjah

Reputation: 6132

Use the UISearchBar property, declare it using

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,30)];

and add it to your UIViewController's view as a subview.

After that, use the searchbar delegate methods in your view controller:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

Check out this tutorial to get a hang of the search bar and its delegates!

EDIT: This method isn't about using a searchbar in tableview itself, but above it. That means you have to put tableView as a subview of a UIViewController, and the searchBar as a subview of the same UIViewController as well!

Upvotes: 9

Related Questions