无法使用NSPredicate对我的表数据实施搜索过滤器

时间:2013-04-27 15:48:45

标签: ios uitableview search filter nspredicate

现在我的搜索工作由sectionsTitle完成,如果我写了#34; Category1"或"类别2"它找到了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; 
}

我的数据结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

使用简化的数据集(只有字典中包含的名称条目),这应该重现您的设置:

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

然后NSPredicate的ANY运算符将找到您所追求的数组:

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

要了解此谓词如何过滤您的类别数组:

NSArray *filteredCats = [categories filteredArrayUsingPredicate:catPred];
BTW,如果您构建一些自定义对象来保存数据而不是仅依赖于数组和字典,那么您可能会变得更加困惑。