使用 NSPredicateEditor 强制显示复合行
我正在代码中创建一个 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];
I'm creating an NSPredicateEditor in code and trying to achieve what I had hoped would be a fairly simple task. Basically, I would like to display a compound NSPredicateEditorRowTemplate (giving the user the option to perform matches when "All/Any/None of the following are true") and a number of fairly basic subrows beneath it. To do this, I construct my NSPredicateEditor and bind its value in order to save changes as the user edits their predicate.
The issue I'm experiencing is that, no matter what, I cannot make a compound NSPredicateEditorRowTemplate show by default as the parent row with a single subrow. When I create the predicate initially I pass a bogus empty predicate, like so:
filename BEGINSWITH[cd] ''
In order to force the display of a compound row I have to create two subrows:
filename BEGINSWITH[cd] '' && path ==[cd] ''
For reference, here's how I'm constructing the 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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是你给它一个比较谓词。你需要给它一个复合的。
我猜您的
SIPredicateStringValueTransformer
对象正在获取一个字符串并将其转换为谓词,对吗?它可能会做这样的事情:您想要做的是保证值转换器不只返回任何旧谓词,而是返回一个复合谓词(即
NSCompoundPredicate 而不是
NSComparisonPredicate
)。执行此操作的方法非常简单:当您向谓词编辑器提供
NSCompoundPredicate
时,它会显示复合行。当您仅给出比较谓词时,它仅显示适当的比较行。The problem is that you're giving it a comparison predicate. You need to give it a compound one.
I'm guessing that your
SIPredicateStringValueTransformer
object is taking a string and turning it into a predicate, right? And it's probably doing something like this:What you want to do instead is guarantee that the value transformer doesn't just return any old predicate, but rather a compound predicate (ie an
NSCompoundPredicate
instead of anNSComparisonPredicate
). The way to do that is quite simple:When you give an
NSCompoundPredicate
to the predicate editor, it displays the compound row. When you only give a comparison predicate, it only displays the appropriate comparison row.