我将我的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方法,它确实到达了那个方法。当我运行苹果的示例代码TableSearch时,上面复制的searchDisplayController委托方法确实会运行。所以对我来说,我试图在搜索栏中输入文本,但我的应用程序崩溃了,因为过滤后的列表中没有对象,因为searchDisplayController委托从未被调用过。
有什么想法?谢谢!
发布于 2012-08-17 23:35:43
我只是在苹果的参考资料中看到:
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;我没有在你的代码中看到searchController.delegate = self,这不是必要的吗?
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html
发布于 2013-06-09 22:54:11
确保在头文件中为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。
https://stackoverflow.com/questions/12008701
复制相似问题