UISearchDisplayController shows Header Section of

2019-06-27 13:42发布

问题:

I am facing strange issue with UITableViewController. I display search result in UISearchDisplayController. If I have some data in search result then it is fine, but when I have empty result it will show section of UITableView which resides behind this SearchDC. I have same delegate and source for both Search and normal table view. Refere images attached with post(one is at beginning and one at end).I have colde like below:

    - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *pstrHeader = [[NSString alloc] init];
    if(self.isSearchActivated)
    {
        NSInteger count =  [self.parrSearchDataSourceKeys count];
        if(count > 0)
            pstrHeader = [pstrHeader stringByAppendingString:[self.parrSearchDataSourceKeys objectAtIndex:section]];
        else
        {
            pstrHeader = nil;
           [[[UIAlertView alloc]initWithTitle:@"header of section" message:@"Count is zero" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];

        }

    }
    else
    {
        NSInteger count = [self.parrDataSourceKeys count];
        if(count > 0)
            pstrHeader = [pstrHeader stringByAppendingString:[self.parrDataSourceKeys objectAtIndex:section]];
        else
            pstrHeader = nil;

    }
    return pstrHeader;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    NSInteger count = 0;
    if(self.isSearchActivated)
    {
        count =  [self.parrSearchDataSourceKeys count];
        if(count <= 0)
        {
            [[[UIAlertView alloc]initWithTitle:@"number of section" message:@"Count is zero" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
        }
    }
    else
        count = [self.parrDataSourceKeys count];
    return count;

}

I have same class for delegate and source but internally it will handle different maps and arrays for normal table view and search result table view.

Also I display search result like below:

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

    NSString *selectedTitle = [searchBar.scopeButtonTitles objectAtIndex: searchBar.selectedScopeButtonIndex];
    selectedTitle = [self convertStringToEnglish:selectedTitle];
    self.isSearchActivated = YES;

    FolderInfo* searchFolder = [[FolderInfo alloc] init];
    if(self.pmdSearchDataSource != nil)
    {
        [self.pmdSearchDataSource removeAllObjects];
    }
    if(self.parrSearchDataSourceKeys != nil)
    {
        [self.parrSearchDataSourceKeys removeAllObjects];
    }
    NSString* pstrSearchText = [searchBar text];
    NSString* pstrSearchIn = @"Folder";
    if([self.pstrWorkspaceId isEqualToString:@"-1"])
    {
        pstrSearchIn = @"Application";

    }
    else if([self.pstrFolderId isEqualToString:@"-1"])
    {
        pstrSearchIn = @"Project";
    }

    self.pmdSearchDataSource = [[NSMutableDictionary alloc] initWithDictionary:[searchFolder GetSearchListForSubElementFor:pstrSearchText InWorkspaceId:self.pstrWorkspaceId
                                                                                                                InFolderId:self.pstrFolderId forSearchField:selectedTitle In:pstrSearchIn] copyItems:NO ];
    NSArray *keys = [[self.pmdSearchDataSource allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.parrSearchDataSourceKeys = [[NSMutableArray alloc] initWithArray:keys];

    self.pSearchDC.searchResultsTableView.delegate = self;
    self.pSearchDC.searchResultsTableView.dataSource = self;
    [self.view bringSubviewToFront:self.pSearchDC.searchResultsTableView];
    [self.pSearchDC.searchResultsTableView reloadData];
}

If intially normal table view have two sections visible on screen and then i move to searchResultDC it will first show blank table but after search clicked when I have no result and reload searchResultDC tableview it will show section of normal table view which is behnind this searchResultDC

I have refered post: Reloading UITableView behind UISearchDisplayController

But note that I need section in search result also and solution in above post is for case where we dont have section in search result.

Let me know if you need more information.