我正在用代码创建一个NSPredicateEditor,并试图实现我所希望的相当简单的任务。基本上,我想要显示一个复合NSPredicateEditorRowTemplate (当“所有/任何/没有下列条件为真”时,允许用户选择执行匹配)和它下面的一些相当基本的子行。为此,我构造了NSPredicateEditor并绑定了它的值,以便在用户编辑谓词时保存更改。
我遇到的问题是,无论如何,我都不能默认地将复合NSPredicateEditorRowTemplate显示为只有一个子行的父行。当我最初创建谓词时,我传递了一个虚假的空谓词,如下所示:
filename BEGINSWITH[cd] ''为了强制显示复合行,我必须创建两个子行:
filename BEGINSWITH[cd] '' && path ==[cd] ''作为参考,下面是我是如何构造NSPredicateEditor的:
NSArray *keyPaths = [NSArray arrayWithObjects:[NSExpression expressionForKeyPath:@"filename"], [NSExpression expressionForKeyPath:@"path"], nil];
NSArray *operators = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSEqualToPredicateOperatorType],
[NSNumber numberWithInteger:NSNotEqualToPredicateOperatorType],
[NSNumber numberWithInteger:NSBeginsWithPredicateOperatorType],
[NSNumber numberWithInteger:NSEndsWithPredicateOperatorType],
[NSNumber numberWithInteger:NSContainsPredicateOperatorType],
nil];
NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:keyPaths
rightExpressionAttributeType:NSStringAttributeType
modifier:NSDirectPredicateModifier
operators:operators
options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];
NSArray *compoundTypes = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSNotPredicateType],
[NSNumber numberWithInteger:NSAndPredicateType],
[NSNumber numberWithInteger:NSOrPredicateType],
nil];
NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];
NSArray *rowTemplates = [NSArray arrayWithObjects:compound, template, nil];
[template release];
[compound release];
predicateEditor = [[NSPredicateEditor alloc] init];
[predicateEditor setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[predicateEditor setRowTemplates:rowTemplates];
[predicateEditor setCanRemoveAllRows:NO];
[predicateEditor setContinuous:YES];
[predicateEditor setFrame:[[scrollView contentView] bounds]];
// Create a custom binding for our NSPredicateEditor
SIPredicateStringValueTransformer *transformer = [[[SIPredicateStringValueTransformer alloc] init] autorelease];
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionaryWithObjectsAndKeys:transformer, NSValueTransformerBindingOption, [NSNumber numberWithBool:YES], NSValidatesImmediatelyBindingOption, nil];
[predicateEditor bind:@"value" toObject:self withKeyPath:@"self.filter.predicate" options:bindingOptions];
// Add our NSPredicateEditor to our NSScrollView
[scrollView setDocumentView:predicateEditor];发布于 2011-05-13 00:32:46
问题是你给了它一个比较谓词。你需要给它一个复合的。
我猜您的SIPredicateStringValueTransformer对象正在接受一个字符串并将其转换为谓词,对吗?它可能是这样做的:
- (id) transformedValue:(id)value {
return [NSPredicate predicateWithFormat:value];
}相反,您想要做的是保证值转换器不只返回任何旧的谓词,而是一个复合谓词(即NSCompoundPredicate而不是NSComparisonPredicate)。实现这一点的方法非常简单:
- (id) transformedValue:(id)value {
NSPredicate *p = [NSPredicate predicateWithFormat:value];
if ([p isKindOfClass:[NSComparisonPredicate class]]) {
p = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:p]];
}
return p;
}当您向谓词编辑器提供NSCompoundPredicate时,它将显示复合行。当您只给出比较谓词时,它只显示相应的比较行。
https://stackoverflow.com/questions/5976244
复制相似问题