OK, this is kind of complex (or maybe I'm just making it complex) but here goes... I have a CoreData model which has two entities. The first entity (called "MasterItem") has a to-many relationship (called "subItems") to the other entity (called "SubItem"). The MasterItem also has a to-one relationship (called "currentSubItem") to the SubItem entity. This allows each master item to contain it's own sub items and a selection of the sub item in use. I have a table view which lists all master items (one per row). In this table view I have a "subItems" pop-up menu column. Bound like so...
content: Bind to: MasterItem Array Controller Controller Key: arrangedObjects Model Key Path: subItemsArray
contentValues: Bind to: MasterItem Array Controller Controller Key: arrangedObjects Model Key Path: subItemsArray.displayName
selectedObject: Bind to: MasterItem Array Controller Controller Key: arrangedObjects Model Key Path: currentSubItem
NOTE: As a work around to populate the menu I have sub-classed NSManagedObject for my MasterItem entity and provided the subItemsArray accessor - see: Cocoa-dev thread "CoreData and arrays owned by arrays..."
- (NSArray *)subItemsArray { return [self valueForKeyPath:@"subItems.@allObjects"]; }
Each master item and sub item has a "value" attribute of type float. I need to display the difference of the master item's value from the currently selected sub item's value in a table column. Currently I am doing this by adding another accessor to my MasterItem entity called difference.
- (NSNumber *)difference { NSNumber *subItemValue = [self valueForKeyPath:@"currentSubItem.value"]; NSNumber *masterItemValue = [self valueForKeyPath:@"value"]; NSNumber *difference = [NSNumber numberWithFloat:[subItemValue floatValue] - [masterItemValue floatValue]]; return (difference); }
And binding the table column like so... value: Bind to: MasterItem Array Controller Controller Key: arrangedObjects Model Key Path: difference
This all works out nice and good, expect for one case. When the user changes the currently selected sub item by using the pop-up menu the difference value isn't updated. It only updates when the user chooses the change any other value of either the master or sub item.
I'm thinking I need to add a observer for the currently selected sub item and update the difference value manually when it changes. However I just wanted an opinion or two on if I'm going about this problem in the right way or not?
Thanks, Lee |