Question

I'm trying to mimic the behaviour of a table view similar to the iPod app for Artists - it's a sectioned table view with a section index on the right, with a search bar at the top, but initially hidden when view shown.

I am using sdk 3.1.2 and IB, so simply dragged a UISearchDisplayController into my NIB - it wires everything up for searching. The problem starts because I'm adding the UISearchBar to the first section of the UITableView, because if I understand correctly I must do this so I can jump to the search bar by touching the search icon in the section index directly? When the table view appears I see the search bar but it has resized and I now have a white block behind the section index at the top. It doesn't take the color of the UISearchBar's surround which interestingly is different to that shown in Interface Builder.

I found a tip to add a small navigation bar and a UISearchBar in a UIView, then add this to the table view cell. This works, but the color of the navigation bar's background is what you'd expect normally (gray), not the different color as noted above. More interesting, if I tap the search bar to start a search, then tap Cancel, all is fixed. The background along the whole tableview cell when the search bar is, is the same?

Was it helpful?

Solution

Answered my own question, but might be helpful to other beginners:

  • Put the search bar in the table view header when view loads, scroll contentOffset down the height of this search bar (typically 44 pixels), but easy to check dynamically
  • Add a search icon in the section index with "{search}"
  • Make the sectionIndexSection for the search -1
  • When handling section index touches use the 'special' index value for search "-1" (to scroll the contentOffset back to (0, 0)

Update: use the string constant UITableViewIndexSearch string in place of the undocumented string "{search}" - i.e. return it as one of the array items when implementing:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

in UITableViewDataSource.

OTHER TIPS

Put the search Controller in the beginning of the tableView and try this:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index==0){
        [tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        return -1;
    }
...
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        NSMutableArray * retValue =[NSMutableArray arrayWithArray:[YOUR ARRAY]];
        [retValue insertObject:UITableViewIndexSearch atIndex:0];
       return retValue;
 } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top