UISearchDisplayController searchResultsTableView没有隐藏在iOS7中

问题描述 投票:3回答:2

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

self.searchDisplayController.searchResultsTableView.hidden = YES;

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

编辑:

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

-(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];
}
iphone ios objective-c ios7 xcode5
2个回答
0
投票

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

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

要么

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

[self searchBarCancelButtonClicked:searchBar];

它会调用UISearchBar searchBarCancelButtonClicked委托和

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

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

我希望它会帮助你......!


0
投票

你知道搜索栏是否有焦点。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_searchBar isFirstResponder]) {
              return [_filteredData count];
    } else {

        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];

    }
}

配置单元格时,请执行相同的问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"CustomCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Entidad *e = nil;

    if ([_searchBar isFirstResponder])
    {
        e = [self.filteredData objectAtIndex:indexPath.row];

    }
    else
    {
        e = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }

    cell.nameLabel.text = e.titulo;
    cell.thumbnailImageView.image = [UIImage imageNamed:@"password-50.png"];
    cell.dateLabel.text = e.sitio;

    return cell;
}

设置过滤器参数

-(void)filter:(NSString*)text
{

    _filteredData = [[NSMutableArray alloc] init];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];


    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"titulo" ascending:YES];
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];


    if(text.length > 0)
    {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text];
        [fetchRequest setPredicate:predicate];
    }

    NSError *error;


    NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    _filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities];

    NSLog(@"%@", _filteredData);

    [self.tableView reloadData];
}

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    [self filter:text];
}

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

    [self filter:@""];

    [_searchBar resignFirstResponder];
    [_searchBar setText:@""];
    [_tableView reloadData];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

    [_searchBar setShowsCancelButton:YES animated:YES];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [_searchBar setShowsCancelButton:NO animated:YES];
}

我希望能为你服务,我的英语非常有限

© www.soinside.com 2019 - 2024. All rights reserved.