0

検索バーに簡単に移動できるように、検索用の画像 (虫眼鏡) をセクションとして追加しようとしています。

これは私が経験している問題です: ここに画像の説明を入力

セクションをセットアップするための私のコードは次のとおりです。

注: self.fbFriends は、ユーザーの Facebook の友達を含む辞書の配列です。

self.fbFriends = [[NSArray alloc] initWithArray:[[MESCache sharedCache] facebookFriends]];
self.searchResults = [[NSMutableArray alloc] init];
self.sections = [[NSMutableDictionary alloc] init];

BOOL found;

[self.sections setValue:[[NSMutableArray alloc] init] forKey:UITableViewIndexSearch];

// Loop through the friends and create our keys
for (NSDictionary *friend in self.fbFriends)
{
    NSString *c = [[friend objectForKey:@"name"] substringToIndex:1];

    found = NO;

    for (NSString *str in [self.sections allKeys])
    {
        if ([str isEqualToString:c])
        {
            found = YES;
        }
    }

    if (!found)
    {
        [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
    }
}

// Loop again and sort the friends into their respective keys
for (NSDictionary *friend in self.fbFriends)
{
    [[self.sections objectForKey:[[friend objectForKey:@"name"] substringToIndex:1]] addObject:friend];
}

// Sort each section array
for (NSString *key in [self.sections allKeys])
{
    [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
}

これが私のセクション設定とヘッダービューです:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return 1;
    } else {
        return [[self.sections allKeys] count];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return NSLocalizedString(@"FbFriendsSearchControllerSection", @"Fb Friends search controller - section title for search results table view");
    } else {
        return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
    }
}

これがどのように間違っているか、誰にもわかりますか?

4

1 に答える 1

0

これは標準的な方法ではありません。検索を表示するためだけに余分なセクションは必要ありません。

「検索」セクションをself.sections辞書に追加しないでください。代わりに、実際のデータのセクションだけがあります。次に、tableView:sectionForSectionIndexTitle:atIndex:メソッドで次のことができます。

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSInteger res = 0;
    if (tableView == self.tableView) {
        if (index == 0) {
            // If the user taps the search icon, scroll the table view to the top
            res = -1;
            [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
        } else {
            res = ... // return the proper index for your data
        }
    } else {
        res = 0;
    }

    return res;
}

補足-辞書の配列が本当に必要です。メイン配列はセクションを表す必要があります。現在のように、常にメイン辞書のキーを取得して並べ替えています。これは何度も行われます - すべて不必要です。配列を使用すると、セクション インデックスからデータをすばやく取得できます。

于 2013-05-20T23:30:42.420 に答える