1、加的是searchDisplayController 可以在storyboard中看到 seachDisplay
2、两个代理 UISearchBarDelegate,UISearchDisplayDelegate 并设置
3、连线 searchDisplay和searchBar
4、将 self.tableView.tableHeaderView=self.searchBar 固定住
5、搜索时调用的方法
#pragma mark 搜索方法
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
NSPredicate * query=[NSPredicate predicateWithFormat:@"SELF.name contains[c]%@",searchString];
self.searchResult=[self.contacts filteredArrayUsingPredicate:query];
return YES;
}
6、搜索的时候会显示自己的tableView 确定是不是searchDisplay.searchResultsTableView
7、修改tableView count
8、跳转判断 查看联系人
#prama mark 点击搜索后搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString * searchString=self.searchBar.text;
NSPredicate * query=[NSPredicate predicateWithFormat:@"SELF.title contains[c]%@",searchString];
self.searchResult=[self.searchData filteredArrayUsingPredicate:query];
NSLog(@"self.searchResult%@",self.searchResult);
[self.searchDisplay.searchResultsTableView reloadData];
}
/*
#pragma mark 实时搜索
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
NSPredicate * query=[NSPredicate predicateWithFormat:@"SELF.title contains[c]%@",searchString];
self.searchResult=[self.searchData filteredArrayUsingPredicate:query];
return YES;
}
*/
参考资料
http://www.cnblogs.com/lesliefang/p/3929677.html