【发布时间】:2012-01-18 14:38:06
【问题描述】:
我的表格视图在这个用户界面中:
但是当我开始搜索时,视图会更改为:
我希望当用户开始搜索时,视图应该与我为不同字段使用的标题相同,例如标题应该存在并且搜索的数据应该显示在其位置的相同标题下?
有可能吗?
我正在使用此代码:
在 viewDidLoad 中:
// create a filtered list that will contain products for the search results table.
//filteredListItems = [NSMutableArray arrayWithCapacity:[listItems count]];
filteredListItems = [[NSMutableArray alloc] initWithCapacity:[self.listVehicles count]];
// restore search settings if they were saved in didReceiveMemoryWarning.
if (self.savedSearchTerm){
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[listTable reloadData];
然后是另一种方法:
-(void)viewDidDisappear:(BOOL)动画{
self.searchWasActive = [self.searchDisplayController isActive];
self.savedSearchTerm = [self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];}
在搜索栏方法中:
//search bar implementation
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.filteredListItems = [self.listVehicles filteredArrayUsingPredicate:resultPredicate];}
- (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;}
谢谢
【问题讨论】:
-
我不明白您要完成什么...请尝试用不同的词再次解释
-
查看搜索前和搜索后的区别,搜索前您可以看到标题 A、B、C,而搜索后该视图被隐藏并出现简单表格。我只想在搜索时或搜索完成时拥有与标题相同的视图
标签: iphone objective-c ios