user542584
user542584

Reputation: 2699

UISearchview responding to actions - TableSearch sample iphone

I would like to modify the tableSearch sample code, so that there is no nib for the tableview. As it is already a tableviewcontroller subclass, I can call the tableview like this:

MainViewController *mainViewController = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];//NibName:@"MainView" bundle:nil];

Without the nib, I would like to add the search bar manually. I tried below:

    UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,10, 320, 41)];
[searchBar setFrame:CGRectMake(0,10, 320, 41)];
searchBar.delegate = self;
searchBar.tintColor = [UIColor redColor];
[searchBar sizeToFit];
self.tableView.tableHeaderView  = searchBar;

The searchbar does appear ok. Now I would like the search action of this searchbar to point to the methods that I already have in my code, i.e searchDisplayController delegate methods. Is there anyway I can point the self.searchDisplayController's Search bar property to my custom searchbar, so they do the same actions? Thanks in advance...

Upvotes: 0

Views: 509

Answers (2)

Tatarasanu Victor
Tatarasanu Victor

Reputation: 654

Hm.. this is code from one of my VC. Also set UISearchBarDelegate,UISearchDisplayDelegate

_listContainer = [[UITableView alloc] init];
_listContainer.delegate = self;
_listContainer.dataSource = self;
_listContainer.separatorStyle = UITableViewCellSelectionStyleNone;
_data = [[NSArray alloc] init];

And then

_searchBar = [[NL_CustomSearchBar alloc] initWithFrame:CGRectMake(4.0f, 0, 150, 46)];
 _searchBar.delegate = self;
 _listContainer.tableHeaderView = _searchBar;

Upvotes: 0

Mundi
Mundi

Reputation: 80273

You have to use the initWithSearchBar:contentsViewController method of UISearchDisplayController. This way, your custom search bar will be associated with the controller. Also you have to set the contentsViewController in this method.

Upvotes: 1

Related Questions