UITableView和SearchBarController查看iPhone

时间:2012-01-18 14:38:06

标签: iphone objective-c ios

我的tableview位于此用户界面中:

enter image description here

但是当我开始搜索时,视图会更改为:

enter image description here

我希望当用户开始搜索时,视图应该与我在不同字段中使用的标题相同,例如标题应该在那里,搜索的数据应该显示在相同的标题下?

有可能吗?

我正在使用此代码:

在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;}

由于

1 个答案:

答案 0 :(得分:0)

您应该在没有(!!!) SearchDisplayController的情况下实现UISearchBar 。转到您的xib文件,如果您在右侧选项卡中搜索“搜索栏”,您将获得2个结果。你应该只为你的项目icacorate searchBar。

另一个解决方案,只需将NO返回到:

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