开发者

Howto implement UISearchDisplayController in a UIViewController without Interface Builder

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];

[mySearc开发者_如何学运维hDisplayController 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 ?


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜