我已经为我的 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/