Forcing the display of a compound row with NSPredicateEditor

安稳与你 提交于 2019-12-22 06:57:04

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/5976244/forcing-the-display-of-a-compound-row-with-nspredicateeditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!