0

sectionsTitle「Category1」または「Category2」と書くと、セクションCategory1またはCategory2が見つかりますが、ここから、このすべてのセクションでNAMESで検索する必要があります。

NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
[NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]];

名前で検索するには、コードで何を変更する必要がありますか? 今、私はNSArray、NSMutableArray、およびNSDictionaryのすべてと混同しています:(

次のようにデータをロードします。

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];

sectionKeys = [NSMutableArray new];
sectionsTitle = [NSMutableArray new];


        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"])
        {

            ann = [dict objectForKey:@"Category1"];
            [resultArray addObject:@"Category1"];
            [resultDic setValue:ann forKey:@"Category1"];
            [sectionKeys addObject:@"Section 1"];

        }


        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yellowKey"])
        {
            ann = [dict objectForKey:@"Category2"];
            [resultArray addObject:@"Category2"];
            [resultDic setValue:ann forKey:@"Category2"];
            [sectionKeys addObject:@"Section 2"];

        }


self.tableData = resultDic;
self.sectionsTitle = resultArray;

[myTable reloadData];

これは私がデータをフィルタリングする方法です:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];
    searchResults = [sectionsTitle filteredArrayUsingPredicate:resultPredicate];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

これは私のテーブルがどのように見えるかです:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return 1;
        }else{
        return sectionKeys.count;
        }

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return @"Search";
        }else{
            return [sectionKeys objectAtIndex:section];
        }

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        if (tableView == self.searchDisplayController.searchResultsTableView) {

            return [searchResults count];

        } else {
            int num = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];
            return num;
        }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


        if (tableView == self.searchDisplayController.searchResultsTableView) {

             cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];

        } else {
            NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];


            cell.textLabel.font = [UIFont fontWithName:@"Avenir" size: 16.0];
            cell.detailTextLabel.font = [UIFont fontWithName:@"Avenir" size: 12.0];

            cell.textLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Name"]];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"Address"]];

        }

        return cell; 
}

私のデータ構造:

ここに画像の説明を入力

4

1 に答える 1

0

単純化されたデータ セット (辞書に含まれる Name エントリのみ) を使用すると、セットアップが再現されます。

NSArray *categories = @[
    @[@{@"Name":@"Joe"}, @{@"Name":@"Jane"}],
    @[@{@"Name":@"Anne"}, @{@"Name":@"Bob"}]
];

次に、ANYNSPredicate のオペレーターは、目的の配列を見つけます。

NSString *nameToSearch = @"Bob";
NSPredicate *catPred = [NSPredicate predicateWithFormat:@"ANY Name = %@", nameToSearch];

この述語がカテゴリ配列をどのようにフィルタリングできるかを確認するには、次のようにします。

NSArray *filteredCats = [categories filteredArrayUsingPredicate:catPred];

ところで、配列と辞書だけに依存するのではなく、カスタム オブジェクトを作成してデータを保持すると、混乱が少なくなる可能性があります。

于 2013-04-27T16:32:46.063 に答える