[noob] Cocoa - CoreData question

Discussion in 'Mac Programming' started by sebastijan, Jan 2, 2007.

  1. macrumors member

    #1
    OK. First I use CoreData and have one question. How can I sum numbers in NSTableView and show them in NSTextField? Like in this pic:

    [​IMG]

    P.S.: Sorry for my bad English.
     
  2. Guest

    caveman_uk

    #2
    Unless you're doing anything else with the data then Core Data is way overkill for what you're doing. All you need is an array and perhaps use cocoa bindings (ie an NSArrayController) to handle the boilerplate code.
     
  3. macrumors member

    #3
    Can you show me this, please? some sample?

    Thanks in advance!
     
  4. Guest

    caveman_uk

    #4
    There's not much to see in terms of code as basically the only code is the setting up of an NSMutableArray and a small method for summing the contents. All the cocoa bindings stuff is done graphically in interface builder. There's quite a few cocoa bindings examples knocking around on the web (like this one).
     
  5. macrumors member

    #5
    tnx caveman_uk.

    But can i get some example - source or xcode proj - for this? please.

    I know how to use table in coreData and some bindings stuff... but i dont know how to sum numbers in table and show them in TextField.
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    See the attached. This does it without using CoreData as there is no need for that here. The summing is done entirely in the bindings system ( :cool: ). Note that the sum only recalculates when you add or remove an item from the array. I'm sure you can sort this but I'm not about to worry about how to do that now.
     

    Attached Files:

    • Sum.zip
      File size:
      42.3 KB
      Views:
      24
  7. macrumors member

    #7
    tnx robbieduncan for example.

    but how can i sum number on every change i made and not only on add or remove?
     
  8. Moderator

    robbieduncan

    Staff Member

    #8
    As I said I'm not about to deal with that: it's a challenge for you to work out on your own.
     
  9. macrumors member

    #9
    ok. at least some hint?
     
  10. Moderator

    robbieduncan

    Staff Member

    #10
    Read the Apple documentation on Dependent Keys...

    Edit to add: and yes I have checked that this solves the problem. You need to add a single class level method with a single line of code in it...
     
  11. macrumors member

    #11
    Code:
    + (void)initialize {
        [MyNumber setKeys:
            [NSArray arrayWithObjects:@"string", nil]
            triggerChangeNotificationsForDependentKey:@"@sum.intValue"];
    }
    why doesn't this work?
     
  12. Moderator

    robbieduncan

    Staff Member

    #12
    Because @sum is not part of the key within this object. The dependent key in the object is simply intValue. The @sum is a directive to sum across the collection of these objects.
     
  13. macrumors member

    #13
    tnx. it works.
     

Share This Page