【问题标题】:UISearchDisplayController delegate method is not called未调用 UISearchDisplayController 委托方法
【发布时间】:2013-10-29 11:22:21
【问题描述】:

我正在尝试将UISearchBar 添加到我在“nib”文件中定义的表格视图中。我在 IB 中添加了一个搜索栏和搜索显示控制器,链接了搜索栏插座,并将其添加到视图控制器的 '.h' 文件中:

@interface SearchListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>

另外,我实现了以下委托方法:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{
   [self filterContentForSearchText:searchString scope:
   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
   return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption 
{
   [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
   return YES;
}

我发现shouldReloadTableForSearchString: 方法被调用,但是shouldReloadTableForSearchScope: 方法从未被调用,所以我的表格视图数据没有重新加载搜索结果......我会丢失什么?

提前致谢

【问题讨论】:

  • 仅将 UISearchBarDelegate 放入 .h 类还不够,您需要将其设置为 yourserchbar.delegate=self;

标签: ios uitableview uisearchbar uisearchdisplaycontroller uisearchbardelegate


【解决方案1】:

您必须将委托设置为自己。

searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

UISearchDisplayController Class Reference

【讨论】:

  • 注意:UISearchDisplayDelegate 自 IOS 8 起已弃用。改为使用 UISearchDisplayController(如 Rajneesh071 所述)
猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 2015-06-09
  • 2016-07-16
相关资源
最近更新 更多