使用 NSPredicateEditor 强制显示复合行

发布于 2024-11-07 10:10:00 字数 3577 浏览 0 评论 0原文

我正在代码中创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-11-14 10:10:00

问题是你给它一个比较谓词。你需要给它一个复合的。

我猜您的 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 时,它会显示复合行。当您仅给出比较谓词时,它仅显示适当的比较行。

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:

- (id) transformedValue:(id)value {
  return [NSPredicate predicateWithFormat:value];
}

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 an NSComparisonPredicate). The way to do that is quite simple:

- (id) transformedValue:(id)value {
  NSPredicate *p = [NSPredicate predicateWithFormat:value];
  if ([p isKindOfClass:[NSComparisonPredicate class]]) {
    p = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:p]];
  }
  return p;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文