Hide index row when searching for UITableView

I applied the search bar and indexing capability to my view in the table. It works well. However, I noticed that when I click on the search bar, the index is still available, and clicking on it causes unpredictable results. Instead of debugging this, I thought it was easier to hide the index :)

I found references elsewhere to call sectionIndexTitlesForSectionViewand return null. So, when I click in the search box, searchBarTextDidBeginEditingI made an explicit call [self sectionIndexTitlesForSectionView:[self tableView]].

As a result sectionIndexTitlesForSectionView, nil is called and returns, but the index is still present.

Any ideas / suggestions would be greatly appreciated! Tony.

+5
source share
3 answers

nil - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView, , . UISearchDisplayDelegate, .

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    [self.tableView reloadSectionIndexTitles];
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    [self.tableView reloadSectionIndexTitles];
}

sectionIndexTitlesForTableView :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (self.searchDisplayController.active) {
        return nil;
    } else {
        //Add @"{search}", to beginning to add search icon to top of index
        return @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"#"];
    }
}

. , self.searchDisplayController.active if. , tableView != self.tableView.

+23

(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView.

:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (tableView == [self tableView]) {
        return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
    }
    // search view, no index:
    else return nil;
}
0

Here is an easy way if you do not want to pass nil to sectionIndexTitlesForTableView, if you need to make only one index for the search text and want to show the search text as the title of the section title.

NSArray *subViewsOfTblView = [self.tableView subviews];
if([subViewsOfTblView count] > 0)
{
    UIView *indexVw = (UIView*)subViewsOfTblView[[subViewsOfTblView count] - 1];
    if(isSearchON || isFilterON)
        indexVw.hidden = YES;
    else
        indexVw.hidden = NO;
}
0
source

All Articles