问题
Mac OS X. CoreData app. NSTableView controlled by NSArrayController bound to managed object context for the Country entity. The Country entity has a 'name' attribute and a to-many relationship, 'branches', to a Branch entity. The Branch entity has a 'sales' attribute (an NSNumber).
The NSTableView has two NSTableColumns. The first shows the name of the Country. The second should show the total sales for that Country across all its Branches.
The first column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'name'. No problem there.
The second column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'branches.@sum.sales'. This doesn't work. I get the error message: " addObserver:forKeyPath:options:context:] is not supported. Key path: @sum.sales"
If, instead, I add a 'totalSales' method to my Country class and the method is implemented as follows:
- (NSNumber *)totalSales
{
return [[self branches] valueForKeyPath:@"@sum.sales"];
}
and I then bind the column to 'totalSales' it works fine. My understanding of the Collection Operators documentation is that this should be the same as simply binding to 'branches.@sum.sales'. I can't see why the latter doesn't work. Any ideas? I have seen similar questions in this and other forums, but have yet to see an explanation or solution.
回答1:
I don't know if this is still topic for you, but it surely does need an answer.
The second column's value should be bound to NSArrayController in exactly the sam way as first. I don't know why you made it differently and what actually you wanted to achieve.
Your first task was to bind table columns to array columns and this works the same way for all the columns and types.
Second task is to get sum of certain NSTableColumn bound to certain other object, like NSTextfield. And this is done like this:
[totalCountField bind: @"value" toObject: arrayController
withKeyPath:@"arrangedObjects.@sum.price" options:nil];
来源:https://stackoverflow.com/questions/4517145/nstablecolumn-binding-using-collection-operators-like-sum