6

テーブル ビューに検索バーとインデックス機能を実装しました。よく働いています。ただし、検索バーをクリックすると、インデックスがまだ利用可能であり、クリックすると予期しない結果が生じることに気付きました。それをデバッグするよりも、インデックスを非表示にする方が簡単だと思いました:)

sectionIndexTitlesForSectionViewnilを呼び出して返すことへの参照を他の場所で見つけました。そのため、検索ボックスをクリックすると、 がsearchBarTextDidBeginEditing明示的に呼び出され[self sectionIndexTitlesForSectionView:[self tableView]]ます。

その結果sectionIndexTitlesForSectionView、呼び出されて nil が返されますが、インデックスはまだ存在しています。

どんなアイデアや提案も大歓迎です!トニー。

4

3 に答える 3

26

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView投稿に記載されているように、検索がアクティブな場合はnil を返す必要があります。次に、 の 2 つのメソッドをオーバーライドし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", @"#"];
    }
}

self.searchDisplayController.active注: if 条件で使用する必要があることがわかりました。を使用すると機能しませんtableView != self.tableView

于 2012-11-26T22:16:41.487 に答える
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;
}
于 2012-08-13T19:02:42.313 に答える
0

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.

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;
}
于 2016-06-04T09:39:14.527 に答える