I am struggling to accomplish what, in my opinion, will be a relatively common task. I have an NSTableView connected to it by an array through NSArrayController . The array controller has content configured on an NSMutableArray containing one or more instances of the NSObject model class. What I don't know how to do is expose the model inside an NSCell subclass in a way that is convenient.
For purposes of illustration, we will say that the object model is a person consisting of a name, surname, age and gender. Thus, the model will look something like this:
@interface PersonModel : NSObject { NSString * firstName; NSString * lastName; NSString * gender; int * age; }
Obviously, the corresponding classes, getters init, etc. for class.
In my controller class, I define NSTableView , NSMutableArray and NSArrayController :
@interface ControllerClass : NSObject { IBOutlet NSTableView * myTableView; NSMutableArray * myPersonArray; IBOutlet NSArrayController * myPersonArrayController; }
Using Interface Builder I can easily bind the model to the corresponding columns:
myPersonArray --> myPersonArrayController --> table column binding
It works great. Therefore, I delete additional columns, leaving one hidden column attached to the NSArrayController (this creates and maintains a relationship between each row and the NSArrayController ), so I fall into one visible column in the NSTableView and one hidden column. I subclass NSCell and put the appropriate drawing method to create the cell. In my awakeFromNib I set a custom subclass of NSCell :
MyCustomCell * aCustomCell = [[[MyCustomCell alloc] init] autorelease]; [[myTableView tableColumnWithIdentifier:@"customCellColumn"] setDataCell:aCustomCell];
This also works fine in terms of drawing. I get my custom cell displayed in the column and it repeats for each managed entity in the array controller. If I add an object or delete an object from an array controller, the table is updated accordingly.
However ... I had the impression that my PersonModel would be accessible from my NSCell subclass. But I do not know how to get to it. I donβt want to install each NSCell using setters and getters, because then I violate the concept of the whole model by storing data in NSCell instead of referencing it from the array controller.
And yes, I need to have a custom NSCell , so having multiple columns is not an option. Where to from?
In addition to searching Google and StackOverflow, I took a mandatory walk through Apple docs and didn't seem to find an answer. I found many links that hit around the bush, but nothing related to NSArrayController . The controller makes life easier when linked to other elements of the model object (for example, the "master / drilldown" scenario). I also found many links (although there are no answers) when using Core Data, but Im did not use Core Data.
In accordance with the norm, I am very grateful for any help that can be offered!