3
我想在UIViewController
中实现UISearchDisplayController
,因此没有实现UITableView
。我创建了UISearchBar
和UISearchDisplayController
并实现了相应的委托方法。在UIViewController中添加搜索显示控制器
当我运行该应用程序并尝试搜索时,不应显示应与搜索结果一起显示的表视图。为了解释更多我的应用UI,UIViewController
有一张地图,搜索栏放置在导航控制器中。
我现在所理解的,似乎我应该实现一个表视图,以便重新用于搜索结果。但是,没有地方/需要放置表格视图。我能做些什么来解决这个问题?任何提示?
这里是我是如何实现的一切代码:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
_search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[_search setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[_search setPlaceholder:@"Search"];
self.navigationItem.titleView = _search;
_searchArray = [[NSMutableArray alloc] initWithObjects:@"Hi", nil];
_searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:_search contentsController:self];
_searchDisplay.delegate = self;
_searchDisplay.searchResultsDataSource = self;
_searchDisplay.searchResultsDelegate = self;
[_searchDisplay setActive:YES animated:YES];
filteredResults = [[NSMutableArray alloc] init];}
-(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
[filteredResults removeAllObjects]; // clearing filter array
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",text]; // Creating filter condition
filteredResults = [NSMutableArray arrayWithArray:[_searchArray filteredArrayUsingPredicate:filterPredicate]]; // filtering result
NSLog(@"search %@", filteredResults);
}
#pragma mark - UISearchDisplayDelegate Methods
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterForSearchText:searchString scope:[[[[self searchDisplayController] searchBar] scopeButtonTitles] objectAtIndex:[[[self searchDisplayController] searchBar] selectedScopeButtonIndex] ]];
return YES;
}
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
您已经实现了表视图的方法呢?你应该有这个控制器是'searchResultsDataSource'。 – Wain
@很高哦,是的,我没有实现他们,但没有包括他们上面。我确定标识符和阵列用户的行数是正确的。顺便说一下,我的searchResultsDataSource被设置为'self'。你是这个意思吗? – HusseinB
那么哪个方法被调用?搜索?搜索委托将显示?表数据源?什么是记录过滤的搜索结果? – Wain