• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
filterPredicate not effecting NSArrayController
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

filterPredicate not effecting NSArrayController


  • Subject: filterPredicate not effecting NSArrayController
  • From: Erik Stainsby <email@hidden>
  • Date: Wed, 21 Nov 2012 16:27:39 -0800

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: filterPredicate not effecting NSArrayController
      • From: "email@hidden" <email@hidden>
    • Re: filterPredicate not effecting NSArrayController
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: NSFileManager - Incompatible persistent store
  • Next by Date: iPad to Desktop over WiFi
  • Previous by thread: Re: NSFileManager - Incompatible persistent store
  • Next by thread: Re: filterPredicate not effecting NSArrayController
  • Index(es):
    • Date
    • Thread