xcode 搜索 UITableView 时隐藏索引栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11494327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:54:11  来源:igfitidea点击:

Hide index bar when searching UITableView

iosxcodeindexinguisearchbar

提问by Tony

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 sectionIndexTitlesForSectionViewand returning nil. So, when I click in the search box, in searchBarTextDidBeginEditingI have made an explicit call to [self sectionIndexTitlesForSectionView:[self tableView]].

我在别处找到了调用sectionIndexTitlesForSectionView和返回 nil 的参考。因此,当我单击搜索框时,searchBarTextDidBeginEditing我已明确调用[self sectionIndexTitlesForSectionView:[self tableView]].

The result is that sectionIndexTitlesForSectionViewdoes get invoked and return nil, but the index is still present.

结果是sectionIndexTitlesForSectionView确实被调用并返回 nil,但索引仍然存在。

Any ideas/suggestions would be greatly appreciated! Tony.

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

回答by Mike1in3

You have to return nil in - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableViewif you the search is active, as you note in your post. Then, override two methods from the UISearchDisplayDelegateto refresh the indices.

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

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

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

For the sectionIndexTitlesForTableViewmethod, I prefer to use:

对于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", @"#"];
    }
}

NOTE: I found that you must use self.searchDisplayController.activein the if condition. It doesn't work if you use tableView != self.tableView.

注意:我发现你必须self.searchDisplayController.active在 if 条件下使用。如果您使用tableView != self.tableView.

回答by marcel salathe

Simply have the method (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableViewreturn nilwhen you are displaying the search results.

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

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

回答by Anuj Saini

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.

这是一种简单的方法,如果您不想在 sectionIndexTitlesForTableView 中传递 nil,如果您只需要为搜索文本创建一个索引并希望将搜索文本显示为部分标题标题。

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