我以编程方式创建一个UISearchBar和UISearchdisplayController.视图控制器是一个UITableViewController. @interface StockTableViewController()< UISearchdisplayDelegate,UISearchBarDelegate>你可以看到下面的代码.但是当我在搜索栏中输入时,不会调用shouldReloadTableForSearchString,包括其他UISearchdisplayDelegate方法.
- (void)viewDidLoad { [super viewDidLoad]; self.searchResults = [NSArray array]; UISearchBar *searchBar = [[UISearchBar alloc] init]; searchBar.barStyle = UISearchBarStyleDefault; searchBar.searchBarStyle = UISearchBarStyleDefault; searchBar.showsCancelButton = YES; searchBar.showsScopeBar = NO; searchBar.delegate = self; UISearchdisplayController *searchdisplayController = [[UISearchdisplayController alloc] initWithSearchBar:searchBar contentsController:self]; searchdisplayController.delegate = self; searchdisplayController.searchResultsDataSource = self; searchdisplayController.searchResultsDelegate = self; self.tableView.tableHeaderView = searchBar; } - (void)searchdisplayControllerWillBeginSearch:(UISearchdisplayController *)controller { NSLog(@"searching"); //not showed up in the console } - (BOOL)searchdisplayController:(UISearchdisplayController *)controller shouldReloadTableForSearchString:(Nsstring *)searchString { NSLog(@"%@",searchString); //not showed up in the console nspredicate *predicate = [nspredicate predicateWithFormat:@"name LIKE %@",searchString]; NSArray *results = [Stock MR_findAllSortedBy:@"createdDate" ascending:NO withPredicate:predicate]; if ([results count] > 0) { self.searchResults = results; [self.searchdisplayController.searchResultsTableView reloadData]; } return YES; }