搜索UITableView时隐藏索引栏

Ton*_*ony 6 indexing xcode uisearchbar ios

我已经为我的表视图实现了搜索栏和索引功能.运作良好.但是,我注意到当我在搜索栏中单击时,索引仍然可用并单击它会导致不可预测的结果.而不是调试,我认为隐藏索引会更简单:)

我在别处发现了调用sectionIndexTitlesForSectionView和返回nil的引用.所以,当我点击搜索框时,searchBarTextDidBeginEditing我已经明确调用了[self sectionIndexTitlesForSectionView:[self tableView]].

结果是sectionIndexTitlesForSectionView调用并返回nil,但索引仍然存在.

任何想法/建议将不胜感激!托尼.

Mik*_*in3 25

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView如果您在搜索中处于活动状态,则必须返回nil ,如您在帖子中所述.然后,重写两个方法UISearchDisplayDelegate来刷新索引.

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

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    [self.tableView reloadSectionIndexTitles];
}
Run Code Online (Sandbox Code Playgroud)

对于该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", @"#"];
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我发现你必须使用self.searchDisplayController.activeif条件.如果您使用它不起作用tableView != self.tableView.


mar*_*the 0

只需在显示搜索结果时让该方法(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView返回即可。nil

就我而言,这看起来像这样:

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