Filtering A Tree Controller

2020-06-29 23:56发布

问题:

I have now nearly figured out how to Filter a NSTreeController, to do this I have sub-classed NSManagedObject and added some code to my App Delegate, I have also bound my NSSearchField to the filterPredicate of my App Delegate but I think I need to connect my NSTreeController and NSSearchField in some way to make it work. Below I have posted all the code I have used so far to try and make it work.


NSManagedObject Sub-Class Header File.

@interface Managed_Object_Sub_Class : NSManagedObject {
    NSArray *filteredChildren; // this should fix the compiler error
}

- (NSArray *)filteredChildren;


@end

NSManagedObject Sub-Class Implementation File.

@implementation Managed_Object_Sub_Class

static char *FilteredChildrenObservationContext;

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
    if (self = [super initWithEntity:entity insertIntoManagedObjectContext:context]) {
        [[NSApp delegate] addObserver:self forKeyPath:@"filterPredicate" options:0 context:&FilteredChildrenObservationContext];
        [self addObserver:self forKeyPath:@"subGroup" options:0 context:&FilteredChildrenObservationContext];
    }
    return self;
}

// use finalize with GC
- (void)dealloc {
    [[NSApp delegate] removeObserver:self forKeyPath:@"filterPredicate"];
    [self removeObserver:self forKeyPath:@"subGroup"];
    [super dealloc];
}

- (NSArray *)filteredChildren {
    if (filteredChildren == nil) {
        NSPredicate *predicate = [[NSApp delegate] filterPredicate];
        if (predicate)
            filteredChildren = [[[self valueForKey:@"subGroup"] filteredArrayUsingPredicate:predicate] copy];
        else
            filteredChildren = [[self valueForKey:@"subGroup"] copy];
    }
    return filteredChildren;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == &FilteredChildrenObservationContext) {
        [self willChangeValueForKey:@"filteredChildren"];
        [filteredChildren release];
        filteredChildren = nil;
        [self didChangeValueForKey:@"filteredChildren"];
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end

Code Added To App Delegate Header File

NSPredicate *filterPredicate;

Code Added To App Delegate Implementation File

- (NSPredicate *)filterPredicate {
    return filterPredicate;
}

- (void)setFilterPredicate:(NSPredicate *)newFilterPredicate {
    if (filterPredicate != newFilterPredicate) {
        [filterPredicate release];
        filterPredicate = [newFilterPredicate retain];
    }
}

Search Field Binding

alt text http://snapplr.com/snap/vs9q


This doesn't work yet, and so that is why I am asking what I need to do from here to make it work, like I said I think I need to connect the NSSearchField and NSTreeController Together in some way.

回答1:

Again I hav answered my own question, I also hope that this will help other people so they know how to Filter a NSTreeController.

To make it work from my post above do the following.

1.For your entity set the Class as your NSManagedObject Sub-Class in my Case JGManagedObject.

alt text http://dvlp.me/c3k

2.For your search field in IB set the predicate format to what you want to Filter ( The Property in your entity, for me it is name).

alt text http://dvlp.me/9k9rw