【问题标题】:Forcing the display of a compound row with NSPredicateEditor使用 NSPredicateEditor 强制显示复合行
【发布时间】:2011-05-12 09:52:15
【问题描述】:

我正在代码中创建一个 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];

【问题讨论】:

    标签: cocoa xcode nspredicate nspredicateeditor


    【解决方案1】:

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

    我猜你的 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 时,它会显示复合行。当你只给出一个比较谓词时,它只显示相应的比较行。

    【讨论】:

    • 这完全正确。谢谢你这么详细的回答。希望这对将来的人有所帮助。
    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 2017-01-26
    • 2011-12-28
    • 1970-01-01
    • 2015-09-18
    • 2017-06-28
    • 2023-03-18
    相关资源
    最近更新 更多