我设置了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委托永远不会被调用。
思考?谢谢!
答案 0 :(得分:2)
我只是在苹果参考中看到:
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
我在你的代码中没有看到searchController.delegate = self,是不是那个nessesary?
答案 1 :(得分:1)
确保在头文件中为UISearchDisplayController创建iVar。
如果使用以下方法创建UISearchDisplayController:
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
它将由ARC发布,它不会调用任何委托方法,当你调用self.searchDisplayController
(UIViewController的属性)时,它将是nil
。
所以,修复是: 在标题(.h)文件中:
@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
UISearchDisplayController* searchDisplayController;
UISearchBar *searchField;
UITableView* tableView;
NSArray* searchResults;
}
并在实现(.m)文件中:
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
如果按此方式实施,您可以在其余代码中同时调用self.searchDisplayController
和searchDisplayController
。