How to Search Array of Dictionary and show in UITa

2019-05-05 18:17发布

I am pretty new in IOS and I am using UISearchDisplayController for searching.

-(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);
}

And Here is my self.arrProductList is an array of array i.e

    ({
        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;
    })  

My question is how to search using key "pname"? My app is Crashed in

filteredArrayUsingPredicate:

2条回答
女痞
2楼-- · 2019-05-05 18:33

You need to modify your predicate to add the key to look for:

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

Additionally == will actually look for exact matches, ie if you'll enter Band Camp T-Shirt as search then you'll get the result, you won't be able to get any result if you simply enter Band or Camp or Shirt. So in order to achieve the character based search, you need to modify predicate to include contain keyword.

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

[cd] will match with case insensitive.

查看更多
看我几分像从前
3楼-- · 2019-05-05 18:39

My friend solve this issue. Here, is the code:

- (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);
}
查看更多
登录 后发表回答