Reputation:
Is it possible to create a custom controller key for an NSArrayController? There are keys for "arrangedObjects", "selection" etc... Is it possible to implement your own? I can't seem to find any info on this anywhere.
My reason for wanting to do this (besides it seeming like a useful thing to be able to do) is:
I have Entity "Car", with Attributes "color" and "mileage", and an array controller, "CarArrayController" which holds these objects and displays them in a table. Below the table I want two text fields one displaying total red car mileage and the other total blue car mileage. Obviously I can display the total of all the cars: Bind Value To "[email protected]", but so far I have been unable to get separate sums for red and blue cars without implementing more arrays or array controllers.
If I could create a Controller Key "arrangedRedCarObjects" which obviously only returned the red cars, I could have a binding to: "[email protected]".
I can't seem to find any way to achieve my goal :(
There must be a way to get the result I need (it seems like a generic problem), please help!
Cheers,
Oli
Upvotes: 0
Views: 989
Reputation: 96363
The controller keys are more than just strings: They are methods of NSArrayController, each of which goes through some logic, creates an object, and returns that object. You can't just add keys because there wouldn't be any logic behind the key.
So, if you don't want to make more array controllers, you need to make a more capable array controller. Make a subclass of NSArrayController that implements methods for the keys you want, and in those methods, ask yourself for the source array (e.g., arrangedObjects
), perform whatever restriction and rearrangement you want, and return that result.
Don't forget to declare your new methods as dependent on their source properties (e.g., arrangedRedCarObjects
as dependent on arrangedObjects
). That's another thing that just adding keys to a list wouldn't be able to accomplish: It wouldn't know what source property/-ies your new key should depend on.
Upvotes: 1
Reputation: 96363
If you have some way to ask a car whether it is red or blue, you can create two secondary array controllers fed by the primary (bind secondary controller's content array to the primary controller's arrangedObjects
) each with a filter predicate that restricts the array to the proper color of car.
Upvotes: 0