【问题标题】:Can't implement search filter to my table data using NSPredicate无法使用 NSPredicate 对我的表数据实施搜索过滤器
【发布时间】:2013-04-21 15:59:26
【问题描述】:

现在我按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; 
}

我的数据结构:

【问题讨论】:

  • 我编辑了代码并添加了数据结构图片
  • 每个Category都有很多词典Item0、Item1等

标签: ios uitableview search filter nspredicate


【解决方案1】:

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

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];

顺便说一句,如果您构建一些自定义对象来保存您的数据,而不是仅仅依赖数组和字典,您可能不会那么困惑。

【讨论】:

  • 对不起,我不知道如何实现你对我的代码的回答
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 2017-09-26
  • 2012-11-01
  • 1970-01-01
  • 2023-03-23
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多