Re: filtering the values in an NSTableColumn
Re: filtering the values in an NSTableColumn
- Subject: Re: filtering the values in an NSTableColumn
- From: Ken Thomases <email@hidden>
- Date: Thu, 09 Aug 2012 09:47:45 -0500
On Aug 9, 2012, at 7:58 AM, Koen van der Drift wrote:
> Maybe I should use a predicate, something like this:
>
> valueFilter = [NSPredicate predicateWithFormat:@"%@ < value < %@",
> self.minimumValue, self.maximumValue];
> [myArrayController setFilterPredicate: valueFilter];
>
> As I said I'm not able to test it until later today, but I wanted to
> throw it out to see if there are any comments/suggestions. The two
> values are bound to the textfields, so hopefully this will update the
> display automagically when the user changes the value(s).
You should definitely use a predicate but it will not automagically update. The predicate you create above is fixed. The _values at the time of the call_ to -predicateWithFormat: are incorporated into the predicate. The predicate does _not_ incorporate references to the "self" object nor the key paths "minimumValue" or "maximumValue". It can't and that's inherent in the C function call semantics (a.k.a. the Objective-C method call semantics). Arguments are passed by value, not reference. (At a higher conceptual level, you can pass a reference by passing a pointer, but the pointer itself is passed by value.)
You might be able to achieve what you want by making a property on the objects in the array which refers back to the controller and using a predicate like "value between { controller.minimumValue, controller.maximumValue }". However, putting such a reference to the controller is poor design and may be inappropriate for a number of reasons: the same array may be displayed in multiple tables with different controllers, the same objects may be in multiple arrays, etc.
Better would be to create a property on your controller which returns the predicate, constructing it on demand for each call (perhaps with some smart caching). (I'll call the property "minMaxPredicate" for discussion.) Bind the array controller's filterPredicate binding to that property on your controller. Then, arrange for KVO change notifications to be emitted for the property when the minimum and maximum value properties change. The easiest way to do this is to add a method like the following to your class:
+ (NSSet *) keyPathsForValuesAffectingMinMaxPredicate
{
return [NSSet setWithObjects:@"minimumValue", @"maximumValue", nil];
}
See the documentation for the method +keyPathsForValuesAffectingValueForKey: and the section Registering Dependent Keys in the Key-Value Observing Programming Guide to understand why this works.
<https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVODependentKeys.html>
Regards,
Ken
_______________________________________________
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