neowinston
neowinston

Reputation: 7764

How to use UISearchBar to search only the sections titles of a UITableView?

I have an UITableView with sections and many rows.

I don't need a detailed search, going through all cells in the tableview.

All the sections titles are contained inside a NSMutableArray.

Is there a simple way to search only the sections titles of an UITableView, disregarding the cell contents?

Thank you!

Upvotes: 0

Views: 500

Answers (2)

Nirav Bhatt
Nirav Bhatt

Reputation: 6969

See this Apple Sample implementation - notice the function named:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

Here, instead of:

for (Product *product in listContent)

You can put:

for (NSString *sectionHeader in YourSectionArray)

Also change implementation of this function as per your need, you should be done. (like, you can ignore the scope things as you only want to search for section header which is nothing but a simple string) This function is simply search routine which you can change as you wish. It is actually called by following delegate function - this you do not want to change:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

Note that this control I am suggesting is an in-built tableview control that comes with search bar already. It is:

UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>

If you added search bar on your own, you would need to add both these delegates to your UITableView .h file like I did above. If you would rather copy Apple's example implementation, no worries.

Upvotes: 1

fnovelli
fnovelli

Reputation: 68

you have to insert the uisearchbar on the head of the table. next you have to implement a method that search on the array with the titles and create a new array with the elements to show (titles and cells, they can also be two array, you have to choose basing on the normal app-workflow) and reload the table.

Upvotes: 2

Related Questions