• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to sum numbers using NSTableView?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >How to sum numbers using NSTableView? (From: Sebastijan Bauman <email@hidden>)

  • Prev by Date: Re: Switching Contents of NSView
  • Next by Date: Re: Switching Contents of NSView
  • Previous by thread: How to sum numbers using NSTableView?
  • Next by thread: Not receiving notifications on NSArrayController selection changes
  • Index(es):
    • Date
    • Thread