- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width
, 44)];
searchBar.placeholder = @"搜索";
// 添加 searchbar 到 headerview
self.tableView.tableHeaderView = searchBar;
// 用 searchbar 初始化 SearchDisplayController // 并把 searchDisplayController 和当前 controller 关联起来
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
// searchResultsDataSource 就是 UITableViewDataSource
searchDisplayController.searchResultsDataSource = self; // searchResultsDelegate 就是 UITableViewDelegate
searchDisplayController.searchResultsDelegate = self;
}
转载于:https://my.oschina.net/u/554046/blog/532730