Hello list,

I have the following code as the action for an NSSearchField (lifted liberally 
from a CIMGF article)):


- (IBAction) updateFilter:(id)sender
{   
        // clobber any previous value - from SO article - has no effect
    [self.peopleArrayController setFilterPredicate: nil];
    
    NSMutableString * searchText = [NSMutableString 
stringWithString:self.searchField.stringValue];
    
    // Remove extraenous whitespace
    while ([searchText rangeOfString:@"  "].location != NSNotFound) {
        [searchText replaceOccurrencesOfString:@"  " withString:@" " options:0 
range:NSMakeRange(0, [searchText length])];
    }
    //Remove leading space
    if ([searchText length] != 0) [searchText replaceOccurrencesOfString:@" " 
withString:@"" options:0 range:NSMakeRange(0,1)];
    
    //Remove trailing space
    if ([searchText length] != 0) [searchText replaceOccurrencesOfString:@" " 
withString:@"" options:0 range:NSMakeRange([searchText length]-1, 1)];
    
    if ([searchText length] == 0) {
        [self.peopleArrayController setFilterPredicate:nil];
        return;
    }
    
    NSArray * searchTerms = [searchText componentsSeparatedByString:@" "];
    
    if ([searchTerms count] == 1) {
        NSPredicate * p = [NSPredicate predicateWithFormat:@"(firstName 
contains[cd] %@) 
                                OR (lastName contains[cd] %@) OR (organization 
contains[cd] %@)", searchText, searchText, searchText];

        [self.peopleArrayController setFilterPredicate:p];        
    }
    else
    {
        NSMutableArray * subPredicates = [[NSMutableArray alloc] init];
        for (NSString * term in searchTerms) {
            NSPredicate * p = [NSPredicate predicateWithFormat:@"(firstName 
contains[cd] %@) 
                                OR (lastName contains[cd] %@) OR (organization 
contains[cd] %@)", term, term, term];
            [subPredicates addObject:p];
        }
        NSPredicate * cp = [NSCompoundPredicate 
andPredicateWithSubpredicates:subPredicates];

        [self.peopleArrayController setFilterPredicate:cp];
    }
    
    [self.peopleArrayController rearrangeObjects];
    [self.tableView reloadData];
}


The tableView is bound to an arrayController object in XIB, which is in turn a 
ref to this (weak) peopleArrayController in the windowController.

The predicates (whichever is triggered) print to log just as expected.  But the 
contents of the tableView are not reduced.
The table displays the entire contents of the 
peopleArrayController.arrangedObjects unfiltered.

Is there another step required? 
In the bindings inspector > Controller Content Parameters is a filterPredicate 
panel. Do I have to wire this up to a property which holds the current 
predicate to be applied ?

- Erik

OSX 10.8/XC 4.5.2
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to