ios - 搜索 UITableView 时隐藏索引栏

标签 ios xcode indexing uisearchbar

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

我在其他地方找到了调用 sectionIndexTitlesForSectionView 并返回 nil 的引用资料。因此,当我单击搜索框时,在 searchBarTextDidBeginEditing 中,我明确调用了 [self sectionIndexTitlesForSectionView:[self tableView]]

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

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

最佳答案

如果搜索处于事件状态,则必须在 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 中返回 nil,如您在帖子中所述。然后,覆盖 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", @"#"];
    }
}

注意:我发现您必须在 if 条件中使用 self.searchDisplayController.active。如果您使用 tableView != self.tableView,它将不起作用。

关于ios - 搜索 UITableView 时隐藏索引栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11494327/

相关文章:

ios - 触发 AVAudioPlayer 停止所有声音并播放选定的声音,除非单击播放(选定)按钮

Java-交换数组列表索引

python - 使用 if 语句过滤数据?

ios - 在 Swift 中加载插页式广告时出现令人沮丧的内存泄漏

ios - 无法在 iOS 中设置启动图像

ios - 如何在通知内容扩展中单击按钮时打开两个特定的 View Controller ?

iphone - #import "test.h"与 #import <test.h>

database - 索引顺序(升序还是降序)重要吗?

ios - Swift - 存储 View 的堆叠顺序

objective-c - 如何重新创建 Jetsetter 的 iOS 预告片照片和照片查看器幻灯片过渡?