iphone - UISearchDisplayController 搜索结果 TableView 未隐藏在 iOS 7 中

标签 iphone ios objective-c ios7 xcode5

我正在使用 UISearchDisplayController 在 tableview 中显示搜索结果。如果搜索结果为空,我将隐藏 UISearchDisplaycontroller 的 searchResultTableView。它在 iOS 6.0 之前工作正常,但在 iOS 7 中不行。 我正在努力寻找一些解决方案,但不幸的是我还没有找到它。 我正在使用以下语句隐藏 searchResultTableView

self.searchDisplayController.searchResultsTableView.hidden= YES;

以下是 iOS 6 和 iOS 7 中的屏幕截图。

enter image description here enter image description here

编辑:

我正在使用以下 UISearchDisplayController 委托(delegate)方法

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


-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope{
[Appdelegate.arrFilteredDrugSummary removeAllObjects];
[Appdelegate.arrFilteredDrugID removeAllObjects];

for (DrugDetails *drug in Appdelegate.arrDrugSummary)
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(SELF BEGINSWITH [cd] %@)", searchText];
    if(![drug.tradeName isEqual:[NSNull null]])
    {
        [drug.tradeName compare:searchText options:NSCaseInsensitiveSearch];
        BOOL resultTradeName; 
        if((resultTradeName = [predicate evaluateWithObject:drug.tradeName]))
        {
            if (![Appdelegate.arrFilteredDrugID containsObject:drug.ID])
            {
                [Appdelegate.arrFilteredDrugSummary addObject:drug];
                [Appdelegate.arrFilteredDrugID addObject:drug.ID];
            }
        }
    }
}
if (![Appdelegate.arrFilteredDrugSummary count])
{
    [self.btnAddNewDrug setHidden:NO];
    self.tblview.hidden=YES;
    self.searchDisplayController.searchResultsTableView.hidden = YES;
}
else
{
    [self.btnAddNewDrug setHidden:YES];
    self.tblview.hidden=NO;
    self.searchDisplayController.searchResultsTableView.hidden = NO;
}
[self.searchDisplayController.searchResultsTableView reloadData];
}

最佳答案

我也遇到了同样的问题。你可以通过这种方式克服它

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    self.searchDisplayController.searchResultsTableView.hidden = YES;
}

如果找到任何结果,则使用此代码关闭 searchDisplayController

if (yourArray.count != 0) 
{
     // reload your table here
     [self.searchDisplayController setActive:NO animated:YES];
     self.searchDisplayController.searchResultsTableView.hidden = YES;
}

OR

如果找到搜索结果,您可以使用此代码隐藏 searchDisplayController

[self searchBarCancelButtonClicked:searchBar];

它将调用 UISearchBar searchBarCancelButtonClicked 委托(delegate)和

- (void)searchBarCancelButtonClicked:(UISearchBar *)_searchBar {

    if (yourArray.count == 0) {
        self.searchDisplayController.searchResultsTableView.hidden = YES;
    }
    else
    {
        self.searchDisplayController.searchResultsTableView.hidden = NO;
    }
}

希望对您有所帮助...!

关于iphone - UISearchDisplayController 搜索结果 TableView 未隐藏在 iOS 7 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19042820/

相关文章:

ios - 如何使用 NSPredicate 过滤数组中的多个值?

iphone - 在window和iPad之间建立Socket连接

ios - ld : 1 duplicate symbol for architecture arm64

ios - 如何从epub文件中获取封面图片?

ios - 选择图片后自动返回上传View Controller IOS7

ios - 为什么核心数据不是 ORM

objective-c - 核心动画动画层水平翻转

ios - 如何使用 AutoLayout 和 IB 根据设备更改 UITextField 的高度

ios - 删除 UITableViewCell 后,出现空白行

iphone - iPhone 上 UIBarButtonItems 之间的默认空间