如何search字典的数组和显示在UITableview?

我在IOS很新,我正在使用UISearchDisplayController进行search。

 -(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 =“在不耐烦的T恤上合并function”;
         pname =“合并functionT恤”;
        价格=“299.00”;
         qty = 99;
        },
     {
         ID = 2;
        描述=“食者Krylon Bombear被毁坏的T恤杉”;
         pname =“女孩的T恤”;
        价格=“499.00”;
         qty = 99;
     },
     {
         ID = 3;
        描述=“起床的孩子带营地套头衫连帽衫”;
         pname =“Band Camp T恤”;
        价格=“399.00”;
         qty = 99;
     })  

我的问题是如何使用键“pname”进行search ? 我的应用程序崩溃了

filteredArrayUsingPredicate:

您需要修改谓词以添encryption钥以查找:

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

此外, ==实际上会查找完全匹配,也就是说,如果您进入Band Camp T-Shirt作为search,那么您将得到结果,如果您只是inputBandCampShirt ,则无法得到任何结果。 所以为了实现基于字符的search,你需要修改谓词来包含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); }