Reputation: 373
I have implemented a search bar and index capability for my table view. Working well. However, I noticed that when I click in the search bar, the index is still available and clicking on it causes unpredictable results. Rather than debug that, I thought it would be simpler to hide the index :)
I found references elsewhere to calling sectionIndexTitlesForSectionView
and returning nil. So, when I click in the search box, in searchBarTextDidBeginEditing
I have made an explicit call to [self sectionIndexTitlesForSectionView:[self tableView]]
.
The result is that sectionIndexTitlesForSectionView
does get invoked and return nil, but the index is still present.
Any ideas/suggestions would be greatly appreciated! Tony.
Upvotes: 6
Views: 7768
Reputation: 1
Here is the simple way, if you don't want to pass nil in sectionIndexTitlesForTableView, If you have to make only one index for search text and want to show search text as section header 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;
}
Upvotes: 0
Reputation: 481
You have to return nil in - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
if you the search is active, as you note in your post. Then, override two methods from the UISearchDisplayDelegate
to refresh the indices.
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
[self.tableView reloadSectionIndexTitles];
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.tableView reloadSectionIndexTitles];
}
For the sectionIndexTitlesForTableView
method, I prefer to use:
- (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", @"#"];
}
}
NOTE: I found that you must use self.searchDisplayController.active
in the if condition. It doesn't work if you use tableView != self.tableView
.
Upvotes: 25
Reputation: 252
Simply have the method (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
return nil
when you are displaying the search results.
In my case, this looks something like this:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (tableView == [self tableView]) {
return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
}
// search view, no index:
else return nil;
}
Upvotes: 0