我像这样设置了我的 searchDisplayController 和 searchBar:
UISearchBar *aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
self.reportSearchBar = aSearchBar;
_reportSearchBar.tintColor = DARKGRAY_COLOR;
_reportSearchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_reportSearchBar.delegate = self;
[aSearchBar release];
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:self.reportSearchBar contentsController:self];
self.reportSearchDisplayController = searchDisplayCtlr;
self.reportSearchDisplayController.searchResultsDataSource = self;
self.reportSearchDisplayController.searchResultsDelegate = self;
[searchDisplayCtlr release];
UIBarButtonItem *searchBBI = [[UIBarButtonItem alloc] initWithCustomView:_reportSearchBar];
self.reportSearchBBI = searchBBI;
[searchBBI release];
[self.navigationItem setRightBarButtonItem:_reportSearchBBI animated:NO];
我检查了我的 ViewController 是否符合该类以防万一:
if ([self conformsToProtocol:@protocol(UISearchDisplayDelegate)]) {
NSLog(@"conform to search display");
}
我的 ViewController .h 文件有:
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
但是,我在
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}
它永远不会到达那里。我实现了一个 UISearBarDelegate 方法,它确实得到了那个方法。当我运行 Apple 的示例代码 TableSearch 时,上面复制的 searchDisplayController 委托方法会运行。所以对我来说,我尝试在搜索栏中输入文本,但我的应用程序崩溃了,因为过滤后的列表中没有任何对象,因为 searchDisplayController 委托从未被调用。
想法?谢谢!