3

我正在使用 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];
}
4

2 回答 2

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

我希望能为您服务,我的英语很有限

于 2014-05-03T15:29:14.650 回答
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;
    }
}

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

于 2013-10-03T09:43:12.327 回答