【问题标题】:UITableView and SearchBarController view iPhoneUITableView 和 SearchBarController 视图 iPhone
【发布时间】: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


【解决方案1】:

你应该实现 UISearchBar without(!!!) SearchDisplayController。转到您的 xib 文件,如果您在右侧选项卡中搜索“搜索栏”,您将获得 2 个结果。您应该只将搜索栏加入您的项目。

另一种解决方案,只需返回 NO:

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

【讨论】:

  • 我试过你的另一种解决方案,你建议返回NO;但问题依然存在
  • 嘿,Oded Regev!我已经得到了我的问题的答案,你可以在这里查看它,它也可能对你有帮助,@gerry3 给出了很好的答案并用几句话描述了这个问题,查看它---stackoverflow.com/questions/8922456/…
猜你喜欢
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
相关资源
最近更新 更多