コントローラはUISearchBarDelegateプロトコルを実装する必要があります
次に、initメソッドで、これまたは同様のものを追加します
CGRect searchBarRect = CGRectMake(0, 0, self.view.bounds.size.width, 44);
_searchBar = [[UISearchBar alloc] initWithFrame:searchBarRect];
_searchBar.delegate = self;
その後、これらの3つのメソッドを実装し、私の実装のように見えるものを投稿します
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.tableView.allowsSelection = NO;
self.tableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=@"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
/* fire method that does querying with searchBar.text as attribute */
[self some method:searchBar.text];
}