J W
J W

Reputation: 878

UISearchDisplayController, what's the reference to the UITableView ?

If I have multiple UITableViews, and a UISearchBar with UISearchDisplayController, in the UITableView delegate and datasource methods, how do I get a reference to the UITableView for the Search? Thanks.

Upvotes: 0

Views: 1612

Answers (1)

timthetoolman
timthetoolman

Reputation: 4623

get a reference to your UISearchDisplayController either via an outlet connected to your NIB file or when you create the searchBar programmatically. Once you have the reference, in all of your datasource and delegate methods you can just check to see which tableView is being passed and act appropriately. For example:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (aTableView==self.searchDisplayController.searchResultsTableView) {
         // do something with the search data source
         self.searchDisplayController.active=NO;
    }else{
         // do something with the regular data Source
    }

    // present a view or other response to the selected row
}

here is how you might create the search bar and get the reference to the UISearchDisplayController

-(UISearchBar *)searchBar{
    // Create a search bar
    if (searchBar==nil){
        UISearchBar *aSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)] autorelease];
        aSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        aSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        aSearchBar.keyboardType = UIKeyboardTypeAlphabet;
        aSearchBar.barStyle=UIBarStyleBlack;
        self.searchBar=aSearchBar;

        // Create the search display controller
        UISearchDisplayController *aSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] ;
        aSearchDisplayController.searchResultsDataSource = self;
        aSearchDisplayController.searchResultsDelegate = self;
        aSearchDisplayController.delegate=self;
        self.searchDisplayController=aSearchDisplayController;
        [aSearchDisplayController release];
    }
    return searchBar;

}

then add it to your tableView like this:

self.tableView.tableHeaderView = self.searchBar;

Good luck

Upvotes: 2

Related Questions