9

I have a UIViewController which has a grouped UITableView as a property. I instantiate the UITableView in code and don't use IB. I would like to hook up a UISearchDisplayController to it but can't find any example how this could be done.

This what I have. //Have implemented the UISearchDisplayDelegate in the header file

//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;


UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController;  //Error here ?? self.searchDisplayController is ReadOnly and can't assign

[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];

[mySearchDisplayController release];
[myDisplayController release];

This doesn't seem to work, the searchDisplayController propery of the UIViewController seems to be readonly and I can't hook myDisplayController onto it. I'm really not sure if this the right way to do it.

I've been looking all around google to find some tip or tutorial on how to use a UISearchDisplayController in UIViewController. All the examples I could find was how to implement it into UITableViewController using IB, which is not the way I want to use it.

Can anyone explain how I could get this working ?

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
Oysio
  • 3,486
  • 6
  • 45
  • 54

2 Answers2

23

Here's the code that I use. Put this in viewDidLoad of a UIViewController that instantiates it's own UITableView. I think the part you're looking for is to add the search bar as the header view of the table view.

UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
    searchBarPlaceHolder = @"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;


self.theTableView.tableHeaderView = theSearchBar;

UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
                                        initWithSearchBar:theSearchBar
                                        contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • It's an instance of UISearchDisplayController – Rayfleck Sep 24 '12 at 13:37
  • @luyuan: I think `searchController` is `UISearchDisplayController` object and @Rayfleck forgot talk about it. You can create 1 `UISearchDisplayController` object like `UISearchDisplayController` of `UIViewController`. It will work same as `UIViewController` – Tony Dec 04 '13 at 03:10
  • What is searchBarPlaceHolder ? – C0D3 Jan 11 '14 at 21:05
  • Oh, I think its just an NSString. – C0D3 Jan 11 '14 at 21:11
5

See the Apple Docs:

@property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController

Discussion: This property reflects the value of the searchDisplayController outlet that you set in Interface Builder. If you create your search display controller programmatically, this property is set automatically by the search display controller when it is initialized.

Regexident
  • 29,441
  • 10
  • 93
  • 100
zgjie
  • 2,099
  • 2
  • 21
  • 32
  • I don't understand, "If you create your search display controller programmatically," what does this mean? – jeswang Oct 05 '14 at 10:41