How to use NSArrayController

Discussion in 'Mac Programming' started by JuanC, May 20, 2007.

  1. JuanC macrumors newbie

    Joined:
    May 17, 2007
    #1
    Hi to all,

    I want to show in NSTableview some info.

    I see in apple developer documents that it shows using bindings and NSArrayController but i don't get it work.

    This is my situation :

    @interface PersonsList : NSObject
    {
    NSMutableArray *persons;
    NSString *city
    }

    @interface Person : NSObject
    {
    NSString *name;
    NSString *zipCode;
    NSString *address;
    }

    NSMutableArray persons is an array of person objects.

    I want to show in NSTableview the info of Person Object i have in PersonsList NSMutableArray persons.

    Thanks.
     
  2. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    You need to write KVC setters and getters...or use an NSMutableDictionary.
     
  3. JuanC thread starter macrumors newbie

    Joined:
    May 17, 2007
    #3
    Thanks for your reply , but can you give an example of what i have to do?
     
  4. JuanC thread starter macrumors newbie

    Joined:
    May 17, 2007
    #5
  5. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    If you stick with what you've got you need to write a bunch of methods like
    Code:
    -(NSString*) name;
    {
        return name;
    }
    
    -(void) setName:(NSString*) newName;
    {
       [newName retain];
       [name release];
       name=newName;
    }
    
    You need to do that for every public variable that isn't an array.

    OR

    You could do like the examples and change is so you have one variable that's a mutable dictionary then you only need to write one pair of properties/setProperties methods.

    Of course in Objective-C 2.0 you won't have to write all these setters/getters as then we'll have 'properties'. Sadly that will be Leopard only....
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    Can't you create an NSArrayController and use that like you do for CoreData?

    TBH though the table displaying methods are pretty easy, you just implement

    Code:
    -(int)numberOfRowsInTableView:(NSTableView *)aTableView;
    
    and
    Code:
    -(id)tableView:(NSTableView *)aTableView
    objectValueForTableColumn:(NSTableColumn *)aTableColumn
    		   row:(int)row;
    
    In your application controller, then you point the table's "data source" connection to your application controller.

    Then you just use the reloadData method in NSTableView to change what gets displayed in the table.
     
  7. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    Yeah but he was asking about using bindings. You could use the datasource methods and in some cases they're still the best option but that wouldn't be using bindings.
     
  8. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #9
    I know, I think people get obsessed with bindings, they aren't always the best way ;).
     
  9. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #10
    Especially if you're using custom cells in your tableview.

    Bindings are pretty cool if you're doing a master-detail kind of interface however.
     
  10. JuanC thread starter macrumors newbie

    Joined:
    May 17, 2007
    #11
    Thanks , with this i can get NSMutableArray *persons from PersonsList into NSTableView.

    But how to get it work with bindings ?.

    I do this in all objects that isn't an array , but i can't get it work.
     
  11. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    Good, so just use this ;).

    EDIT: As a general tip about programming: If it works, and at a decent speed on the target system it is fine and leave it and work on the next problem, time is finite after all.
     
  12. slooksterPSV macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #13
    In interface builder, you bind the columns of the objects with the array controller and the value the column is supposed to hold. E.g. if your NSArrayController was called Persons, you'd do the bindings for the column (lets say of name) as it's going to bind to Persons; then in the text field it'sll say value and you type in name On one of those sites it shows you how to do it, with pictures and that.
     

Share This Page