Re: How to sum numbers using NSTableView?
Re: How to sum numbers using NSTableView?
- Subject: Re: How to sum numbers using NSTableView?
- From: Quincey Morris <email@hidden>
- Date: Sun, 5 Jul 2009 16:18:58 -0700
On Jul 5, 2009, at 07:43, Sebastijan Bauman wrote:
I am using NSTableView and Core Data and would like to sum numbers
from two columns and show the sum in third column for every row.
Like this:
http://www.shrani.si/f/3r/5h/4MNSm4Th/1/picture-2.png
what is the easiest way to do this?
is there a way to do this in Table Column Bindings / Value
Transformers (like we do for @sum, @count, etc.) or maybe in
xcdatamodel?
There are multiple ways of doing this, of course. Here's a fairly easy
way, assuming your core data row objects are a custom subclass of
NSManagedObject:
1. Add a new method to your subclass:
...
@property (readonly) int sumOf1And2; // or whatever numeric return
type you want
...
- (int) sumOf1And2 {
return self.value1 + self.value2; // assuming you have custom
accessors for the first 2 column values, or use 'valueForKey:' if not
}
2. Also add to your subclass a dependency on the component values:
+ (NSSet*) keyPathsForValuesAffectingSumOf1And2 {
return [NSSet setWithObjects: @"value1", @"value2", nil]; // but use
the correct property names
}
This is necessary to make "sumOf1And2" properly KVO-compliant.
3. Bind your table's 3rd column to the property "sumOf1And2".
That's all it should take.
(All code written in Mail and untried.)
_______________________________________________
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