强制使用NSPredicateEditor显示复合行 [英] Forcing the display of a compound row with NSPredicateEditor

查看:229
本文介绍了强制使用NSPredicateEditor显示复合行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中创建一个NSPredicateEditor,试图实现我所希望的一个相当简单的任务。基本上,我想显示一个复合NSPredicateEditorRowTemplate(当All / Any / None of the following都是true时,给用户选择执行匹配)和一些相当基本的subrows。为此,我构造了NSPredicateEditor并绑定其值,以便在用户编辑其谓词时保存更改。

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.

我遇到的问题是,无论什么,我不能使复合NSPredicateEditorRowTemplate默认显示为具有单个子行的父行。当我最初创建谓词时,我传递一个伪谓词,如下:

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] ''



为了参考,下面是我如何构造NSPredicateEditor: / p>

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];


推荐答案

问题是,谓词。

我想你的 SIPredicateStringValueTransformer 对象正在接受一个字符串,把它变成一个谓词,对吧?这可能是这样的:

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];
}

您想要做的是确保值变换器不只是返回任何旧谓词,而是一个复合谓词(即 NSCompoundPredicate 而不是 NSComparisonPredicate )。方法很简单:

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;
}

当你给一个 NSCompoundPredicate 到谓词编辑器,它将显示复合行。当您只提供一个比较谓词时,它只显示适当的比较行。

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.

这篇关于强制使用NSPredicateEditor显示复合行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆