1.首先定义属性 @property (nonatomic, retain) UISearchDisplayController *searchDisplayController; 2.创建一个UISearchBar UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; [search setPlaceholder:@"搜索"]; [search setAutocapitalizationType:UITextAutocapitalizationTypeNone]; [search setAutocorrectionType:UITextAutocorrectionTypeNo]; 3.将UISearchBar加入UITableHeaderView self.tableView.tableHeaderView = search; 4.最后将UISearchBar加入UiSearchDisplaycontroller self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:search contentsController:self];
5.设置代理
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
注:需配合UINavgationController一起使用