2012-07-15 99 views
5

我为我的表视图实现了搜索栏和索引功能。运作良好。不过,我注意到,当我点击搜索栏时,索引仍然可用,点击它会导致不可预知的结果。而不是调试,我认为这将是更简单的隐藏索引:)搜索时隐藏索引栏UITableView

我发现其他地方引用sectionIndexTitlesForSectionView并返回零。所以,当我在搜索框中点击时,在searchBarTextDidBeginEditing我已经明确地致电[self sectionIndexTitlesForSectionView:[self tableView]]

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

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

+0

有什么意见吗? – Tony 2012-07-17 19:29:14

+0

仍然希望有人能帮助我。谢谢。 – Tony 2012-07-25 16:57:37

回答

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

谢谢你的建议,马塞尔。也许我还不够清楚 - 我也返回零 - 我已经在调试器中确认nil正在从此函数返回 - 但是,索引仍然显示。 – Tony 2012-08-18 21:19:13

+0

然后我觉得我不太了解这个问题。可能最好,如果你发布一些代码。 – 2012-08-20 00:42:48

+0

下面的代码: ' - (NSArray的*)sectionIndexTitlesForTableView:(UITableView的*)的tableView { //如果搜索,没有必要显示索引 如果(isSearchOn) 回零; else //确保索引不与搜索栏输入字段重叠 [(id)searchBar setContentInset:UIEdgeInsetsMake(5,0,5,35)]; return englishIndex; } }' – Tony 2012-08-22 03:07:52

23

你必须在你的搜索是积极的,因为您在您的文章注意- (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。如果您使用​​,则不起作用。

0

这里是简单的方法,如果您不想在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; 
}