2012-07-15 104 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; 
}