Exposing model object using bindings in custom NSCell of NSTableView
Posted
by Hooligancat
on Stack Overflow
See other posts from Stack Overflow
or by Hooligancat
Published on 2010-04-17T06:53:14Z
Indexed on
2010/04/17
7:03 UTC
Read the original article
Hit count: 509
I am struggling trying to perform what I would think would be a relatively common task. I have an NSTableView
that is bound to it's array via an NSArrayController
. The array controller has it's content set to an NSMutableArray
that contains one or more NSObject
instances of a model class. What I don't know how to do is expose the model inside the NSCell
subclass in a way that is bindings friendly.
For the purpose of illustration, we'll say that the object model is a person consisting of a first name, last name, age and gender. Thus the model would appear something like this:
@interface PersonModel : NSObject {
NSString * firstName;
NSString * lastName;
NSString * gender;
int * age;
}
Obviously the appropriate setters, getters init etc for the class.
In my controller class I define an NSTableView
, NSMutableArray
and an NSArrayController
:
@interface ControllerClass : NSObject {
IBOutlet NSTableView * myTableView;
NSMutableArray * myPersonArray;
IBOutlet NSArrayController * myPersonArrayController;
}
Using Interface Builder I can easily bind the model to the appropriate columns:
myPersonArray --> myPersonArrayController --> table column binding
This works fine. So I remove the extra columns, leaving one column hidden that is bound to the NSArrayController
(this creates and keeps the association between each row and the NSArrayController
) so that I am down to one visible column in my NSTableView
and one hidden column. I create an NSCell
subclass and put the appropriate drawing method to create the cell. In my awakeFromNib
I establish the custom NSCell
subclass:
PersonModel * aCustomCell = [[[PersonModel alloc] init] autorelease];
[[myTableView tableColumnWithIdentifier:@"customCellColumn"]
setDataCell:aCustomCell];
This, too, works fine from a drawing perspective. I get my custom cell showing up in the column and it repeats for every managed object in my array controller. If I add an object or remove an object from the array controller the table updates accordingly.
However... I was under the impression that my PersonModel
object would be available from within my NSCell
subclass. But I don't know how to get to it. I don't want to set each NSCell
using setters and getters because then I'm breaking the whole model concept by storing data in the NSCell
instead of referencing it from the array controller.
And yes I do need to have a custom NSCell
, so having multiple columns is not an option. Where to from here?
In addition to the Google and StackOverflow search, I've done the obligatory walk through on Apple's docs and don't seem to have found the answer. I have found a lot of references that beat around the bush but nothing involving an NSArrayController
. The controller makes life very easy when binding to other elements of the model entity (such as a master/detail scenario). I have also found a lot of references (although no answers) when using Core Data, but Im not using Core Data.
As per the norm, I'm very grateful for any assistance that can be offered!
© Stack Overflow or respective owner