微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 未调用UISearchDisplayDelegate方法

我以编程方式创建一个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;
}

解决方法

它不起作用,因为您正在将UISearchdisplayController创建为局部变量,这会导致在viewDidLoad超出范围时将其解除分配.因此,为您的搜索显示控制器创建一个强大的属性,然后它应该正常工作(如果您将属性命名为searchdisplayController,则会遇到命名冲突,因此请将其称为其他内容).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐