我有一个表格视图和一个搜索栏。我必须搜索多个键的值并相应地过滤表。我使用以下代码进行过滤
- (void)updateSearchResultsForSearchController
{
NSString *searchText = self.searchBr.text;
NSMutableArray *searchResults = [self.arrayPriceList mutableCopy];
NSString *strippedString = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *searchItems = nil;
if (strippedString.length > 0) {
searchItems = [strippedString componentsSeparatedByString:@" "];
}
NSMutableArray *andMatchPredicates = [NSMutableArray array];
for (NSString *searchString in searchItems) {
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
// Below we use NSExpression represent expressions in our predicates.
// NSPredicate is made up of smaller, atomic parts: two NSExpressions (a left-hand value and a right-hand value)
// Product SKU
NSExpression *lhsSKU = [NSExpression expressionForKeyPath:@"STOCKUNIT"];
NSExpression *rhsSKU = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicateSKU = [NSComparisonPredicate
predicateWithLeftExpression:lhsSKU
rightExpression:rhsSKU
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicateSKU];
// Product Major group
NSExpression *lhsProductMajorGroup = [NSExpression expressionForKeyPath:@"PRODUCTMAJORGROUP"];
NSExpression *rhsProductMajorGroup = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicateMajorGroup = [NSComparisonPredicate
predicateWithLeftExpression:lhsProductMajorGroup
rightExpression:rhsProductMajorGroup
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicateMajorGroup];
// at this OR predicate to our master AND predicate
NSCompoundPredicate *orMatchPredicates = [NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
[andMatchPredicates addObject:orMatchPredicates];
}
// match up the fields of the Product object
NSCompoundPredicate *finalCompoundPredicate =
[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
searchResults = [[searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy];
// hand over the filtered results to our search results table
self.arrayFilteredPriceList = searchResults;
}
但是在 cellForRowAtIndexPath 中,我必须得到如下值:
cell.lblSku.text = [[dict objectForKey:@"STOCKUNIT"] objectForKey:@"text"];
cell.lblProductMajor.text = [[dict objectForKey:@"PRODUCTMAJORGROUP"] objectForKey:@"text"];
我的问题是,我将如何添加objectForKey:@"text"
以获取值[NSExpression expressionForKeyPath:@"STOCKUNIT"];
?如何添加额外的密钥expressionForKeyPath
?