views:

42

answers:

2

I'm trying to implement a UISearchBar within a UITableView, which behaves like the one in the "Artists" tab of the iPod application.

I have it hiding the navigation bar, and resizing the search box to show the "Cancel" button, etc. but am unable to get it to hide the section index titles.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
  inSearchMode_ = YES; // tell sectionIndexTitlesForTableView: to return nil
  tableView_.scrollEnabled = NO; // works correctly, var is definitely wired
  [tableView_ reloadSectionIndexTitles]; // doesn't call sectionIndexTitlesForTableView:
  ...

Am I missing something? Is UITableView -reloadSectionIndexTitles broken?

Typically my response to this kind of thing would be to call reloadData, but that has the side effect of causing the UISearchBar to lose focus.

+1  A: 

I think approach you want is along these lines (Say you have a ArtistController which you want to make searchable):

  • Add a sub-controller to the ArtistController called ArtistSearchController
  • When the search box is clicked, bring the ArtistSearchController to the front as modal (to hide artists) or add transparency if you still want to show artists in the background.
  • When a search term is entered, created a model for the ArtistSearchController which is the data from ArtistController, filtered using the search term, and then display it in a list view
  • close the modal view when the user hits cancel.

This will save you from manipulating your original controller/nav bar and give it better usability

psychotik
That sounds very much like the way UISearchDisplayController operates, now I've played with the sample.
chrisbtoo
A: 

It seems what I need to do is to use a UISearchDisplayController rather than hand-rolling my own. I was able to modify the "TableSearch" sample to use sections and section headings, and it behaves as I want it to.

chrisbtoo