一、加的是searchDisplayController 能夠在storyboard中看到 seachDisplayhtml
二、兩個代理 UISearchBarDelegate,UISearchDisplayDelegate 並設置spa
三、連線 searchDisplay和searchBar代理
四、將 self.tableView.tableHeaderView=self.searchBar 固定住code
五、搜索時調用的方法orm
#pragma mark 搜索方法htm
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{blog
NSPredicate * query=[NSPredicate predicateWithFormat:@"SELF.name contains[c]%@",searchString];it
self.searchResult=[self.contacts filteredArrayUsingPredicate:query];table
return YES;class
}
六、tableView 肯定是否是searchDisplay.searchResultsTableView
搜索的時候會顯示本身的七、修改tableView count
八、跳轉判斷 查看聯繫人
#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