【发布时间】:2012-01-18 19:24:20
【问题描述】:
我有一个 UITableViewController,我想在顶部添加一个 UISearchBarController,以便它使用不同的表视图(而不是 UITableViewController 的表视图)进行搜索。
如何通过代码而不是 IB 来初始化它?
@interface mySearchController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UISearchDisplayController *aSearchBarController;
@property (nonatomic, retain) UISearchBar *aSearchBar;
@end
- (id)init {
if ((self = [super init])) {
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)];
self.aSearchBar = tempSearchBar;
self.aSearchBar.delegate = self;
[self.aSearchBar sizeToFit];
self.tableView.tableHeaderView = self.aSearchBar;
[self.aSearchBar release];
UISearchDisplayController *tempSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self];
self.searchDisplayController = tempSearchDisplayController;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview uisearchbar