Re: Calculations in a tableview

2011-07-30 Thread Andre Masse
Thanks to all who helped me on and off the list. After a good night sleep and a 
day off coding, I think I finally got it :-)


 On 28/07/2011, at 23:15 , Quincey Morris wrote:
 
 So how does the array controller enter this picture at all? It's a glue 
 object (aka mediating controller) whose function is to sort and filter the 
 model array property. It has no role in sourcing actual data to your table.

This was the key!

Thanks again,

Andre Masse

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Calculations in a tableview

2011-07-28 Thread Andre Masse
Hi,

For example, lets say I have a tableview with 3 columns: quantity, unit price 
and total. I want to calculate total using (quantity * unit price). Pretty 
simple using a datasource but I've no idea how to do that with bindings (using 
an NSArrayController).

Any pointer to an example of this would be greatly appreciated.

Thanks,

Andre Masse
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Thomas Davie

On 28 Jul 2011, at 23:48, Andre Masse wrote:

 Hi,
 
 For example, lets say I have a tableview with 3 columns: quantity, unit price 
 and total. I want to calculate total using (quantity * unit price). Pretty 
 simple using a datasource but I've no idea how to do that with bindings 
 (using an NSArrayController).
 
 Any pointer to an example of this would be greatly appreciated.

To be honest, this, along with the many debugging issues of bindings is exactly 
why I still use data sources.

Bob___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Andre Masse
Well, I may end up doing this…

Thanks

Andre Masse


On 28/07/2011, at 18:52 , Thomas Davie wrote:

 
 On 28 Jul 2011, at 23:48, Andre Masse wrote:
 
 Hi,
 
 For example, lets say I have a tableview with 3 columns: quantity, unit 
 price and total. I want to calculate total using (quantity * unit price). 
 Pretty simple using a datasource but I've no idea how to do that with 
 bindings (using an NSArrayController).
 
 Any pointer to an example of this would be greatly appreciated.
 
 To be honest, this, along with the many debugging issues of bindings is 
 exactly why I still use data sources.
 
 Bob

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Rajendran P
Try value transformers

Thanks
Rajendran P


On 7/29/11 6:18 AM, Andre Masse andre.ma...@videotron.ca wrote:

Well, I may end up doing this...

Thanks

Andre Masse


On 28/07/2011, at 18:52 , Thomas Davie wrote:


 On 28 Jul 2011, at 23:48, Andre Masse wrote:

 Hi,

 For example, lets say I have a tableview with 3 columns: quantity, unit 
 price and total. I want to calculate total using (quantity * unit price). 
 Pretty simple using a datasource but I've no idea how to do that with 
 bindings (using an NSArrayController).

 Any pointer to an example of this would be greatly appreciated.

 To be honest, this, along with the many debugging issues of bindings is 
 exactly why I still use data sources.

 Bob

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/rpichaim%40juniper.net

This email sent to rpich...@juniper.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Quincey Morris
On Jul 28, 2011, at 15:48, Andre Masse wrote:

 For example, lets say I have a tableview with 3 columns: quantity, unit price 
 and total. I want to calculate total using (quantity * unit price). Pretty 
 simple using a datasource but I've no idea how to do that with bindings 
 (using an NSArrayController).

Well, that's the problem right there. You seem to think bindings have something 
to do with array controllers. They don't. (Bindings and array controllers are 
often used together, of course, both on the content side of the array 
controller and on the arrangedObjects side, but that's just a case of two 
things being used to complement each other.)

When working with bindings, it helps to be very diligent in thinking in terms 
of properties.

If you're supplying a table view's cells via bindings, then you must have a 
model property for *each* column. In your case, your data model is (or 
includes) an array property (say, invoiceItems), whose elements are objects 
(of class, say, InvoiceItem) with properties quantity, unitPrice and 
totalPrice. 

If the total is always calculated from the other two properties, it is 
typically a derived property (one whose value is derived on the fly, rather 
than actually stored), but aside from that implementation detail, there's no 
difference in how you use the properties to populate your table.

In code terms, the short answer to your question is:

   + (NSSet) keyPathsForValuesAffectingTotalPrice {
   return [NSSet setWithObjects: @quantity, @unitPrice, nil];
   }
 
   - (NSInteger) totalPrice { // or whatever the correct data type is
   return self.quantity * self.unitPrice;
   }

The second method provides the derived property value. The first method 
provides KVO compliance.

So how does the array controller enter this picture at all? It's a glue object 
(aka mediating controller) whose function is to sort and filter the model 
array property. It has no role in sourcing actual data to your table***.



*** It's certainly possible to *give* it a role, but I would argue that this is 
a terrible idea.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Steve Steinitz
Hi,

On 29 Jul 11, at 10:51am, cocoa-dev-requ...@lists.apple.com wrote:

 For example, lets say I have a tableview with 3 columns: quantity, unit price 
 and total. I want to calculate total using (quantity * unit price). Pretty 
 simple using a datasource but I've no idea how to do that with bindings 
 (using an NSArrayController).
 
 Any pointer to an example of this would be greatly appreciated.

(Using Core Data with tables) I just write a method to calculate quantity * 
price and then bind the third column to that.

Cheers,

Steve___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Andre Masse
Thanks a lot for the clarifications about the role of the NSArrayController. I 
somehow got confused about the whole binding thing, its not so muddy now. Now, 
the example I gave was a bad simplification of the real thing as the 
totalPrice is more a proposedTotalPrice. It's based on a calculation but 
can/will be changed by the user.

So, what I need is a way to calculate a property, but let the user change it if 
he wants. For example, having 3 values  a, b and c (a +  b = c). The user 
enters a value for a or b, then c is calculated. If the user changes c, 
it now lose its dependance on a and b. This would be easy if the 3 values 
were in an NSTextField instead of a tableview as I could add an action to the 
first 2 fields and none to the third.

Is this can be done using bindings or should I stop fighting the framework and 
and use a datasource?

Thanks,

Andre Masse


On 28/07/2011, at 22:13 , Quincey Morris wrote:

 On Jul 28, 2011, at 15:48, Andre Masse wrote:
 
 For example, lets say I have a tableview with 3 columns: quantity, unit 
 price and total. I want to calculate total using (quantity * unit price). 
 Pretty simple using a datasource but I've no idea how to do that with 
 bindings (using an NSArrayController).
 
 Well, that's the problem right there. You seem to think bindings have 
 something to do with array controllers. They don't. (Bindings and array 
 controllers are often used together, of course, both on the content side of 
 the array controller and on the arrangedObjects side, but that's just a 
 case of two things being used to complement each other.)
 
 When working with bindings, it helps to be very diligent in thinking in terms 
 of properties.
 
 If you're supplying a table view's cells via bindings, then you must have a 
 model property for *each* column. In your case, your data model is (or 
 includes) an array property (say, invoiceItems), whose elements are objects 
 (of class, say, InvoiceItem) with properties quantity, unitPrice and 
 totalPrice. 
 
 If the total is always calculated from the other two properties, it is 
 typically a derived property (one whose value is derived on the fly, rather 
 than actually stored), but aside from that implementation detail, there's no 
 difference in how you use the properties to populate your table.
 
 In code terms, the short answer to your question is:
 
  + (NSSet) keyPathsForValuesAffectingTotalPrice {
  return [NSSet setWithObjects: @quantity, @unitPrice, nil];
  }
 
  - (NSInteger) totalPrice { // or whatever the correct data type is
  return self.quantity * self.unitPrice;
  }
 
 The second method provides the derived property value. The first method 
 provides KVO compliance.
 
 So how does the array controller enter this picture at all? It's a glue 
 object (aka mediating controller) whose function is to sort and filter the 
 model array property. It has no role in sourcing actual data to your table***.
 
 
 
 *** It's certainly possible to *give* it a role, but I would argue that this 
 is a terrible idea.
 
 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calculations in a tableview

2011-07-28 Thread Quincey Morris
On Jul 28, 2011, at 20:00, Andre Masse wrote:

 Is this can be done using bindings or should I stop fighting the framework 
 and and use a datasource?

It can be done with bindings***. All you've done is clarified that totalPrice 
isn't a derived property after all. So, you make a regular property (with 
getter and setter), and initialize the instance variable that backs it to the 
default value (items * price).

Or, you can initialize the instance variable to a not set value (like 0 or 
NSNotFound, or something), but then you won't be able to just @synthesize the 
property -- you'll have to write code to return either the default or the 
explicit value, depending on the contents of the instance variable. (Or, you 
can have a BOOL instance variable that says whether the default has been 
overridden, but in that case you'll have to add code to the setter instead.)

And, incidentally, what you are trying to do is in no way fighting the 
framework. You're doing exactly what you're supposed to be doing. :)




*** Again, focus your thinking on *properties*. Ask yourself what properties 
your data model needs to support the user interaction, and it should become 
immediately clear what to do. Bindings are a way of hooking up (in this case) 
your UI to your data model's properties. They don't replace the data model 
design part of the process.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com