如何搜索字典数组并在 UITableview 中显示?

How to Search Array of Dictionary and show in UITableview?

我是 IOS 的新手,我正在使用 UISearchDisplayController 进行搜索。

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

    return YES;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF == %@", searchText];
    NSArray *filtered = [self.arrProductList filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", filtered);
}

这是我的 self.arrProductList 是一个数组,即

    ({
        ID = 1;
        description = "Coalesce Functioning on impatience T-Shirt";
        pname = "Coalesce Functioning T-Shirt";
        price = "299.00";
        qty = 99;
       },
    {
        ID = 2;
        description = "Eater Krylon Bombear Destroyed T-Shirt";
        pname = "Girl's T-Shirt";
        price = "499.00";
        qty = 99;
    },
    {
        ID = 3;
        description = "The Get-up Kids Band Camp Pullover Hoodie";
        pname = "Band Camp T-Shirt";
        price = "399.00";
        qty = 99;
    })  

我的问题是如何使用关键字"pname"进行搜索?我的应用程序在

中崩溃

filteredArrayUsingPredicate:

您需要修改谓词以添加要查找的键:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pname == %@", searchText];

此外,== 会实际查找完全匹配项,即如果您输入 Band Camp T-Shirt 作为搜索,那么您将得到结果,如果您只需输入 BandCampShirt。因此,为了实现基于字符的搜索,您需要修改谓词以包含 contain 关键字。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pname contains[cd] %@", searchText];

[cd] 将不区分大小写匹配。

我的朋友解决了这个问题。这是代码:

- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar
{
    self.searchBar.showsCancelButton = YES;
}
#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    if (searchResults.count != 0) {
        [searchResults removeAllObjects];
    }
    for (int i=0; i< [self.arrProductList count]; i++)
    {
        NSString *string = [[self.arrProductList objectAtIndex:i] valueForKey:@"pname"];
        NSRange rangeValue = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (rangeValue.length > 0)
        {
            NSLog(@"string contains bla!");

            [searchResults addObject:[self.arrProductList objectAtIndex:i]];
        }
        else
        {
            NSLog(@"string does not contain bla");
        }
    }
    NSLog(@"fiilterArray : %@",searchResults);
}