【发布时间】:2014-04-18 08:26:59
【问题描述】:
我想在UIViewController 中实现UISearchDisplayController,因此没有实现UITableView。我创建了UISearchBar 和UISearchDisplayController 并实现了相应的委托方法。
当我运行应用程序并尝试搜索时,应该与搜索结果一起显示的表格视图没有出现。为了更多地解释我的应用程序 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”。是这个意思吗?
-
那么调用了哪些方法呢?搜索?搜索代理会显示吗?表数据源?记录了哪些过滤后的搜索结果?
-
filteredResults 是搜索结果。为了测试,我只添加了一个对象。此外,我创建了一个表格视图并将其添加到视图中并设置了 [self.table reloadData];在 filterForSearchText:scope: 中,搜索工作正常。所以我想问题是我应该添加至少一个表格视图以便它工作,否则它不会。
-
您不需要另一个表,搜索控制器提供了一个。但是你需要看看哪些方法被调用,哪些没有被调用。
标签: ios objective-c uiviewcontroller uisearchbar uisearchdisplaycontroller