2

我像这样设置了我的 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 委托从未被调用。

想法?谢谢!

4

2 回答 2

2

我只是在苹果参考中看到:

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

于 2012-08-17T15:35:42.710 回答
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

于 2013-06-09T14:54:10.913 回答