I created a subclass of NSTableCellView to do some custom drawing. The table’s content is obtained through a binding to an NSArrayController, thus, new instances of my NSTableCellView subclass are created ‘automatically’ when new data are added to the NSArrayController. I need some code to run once when a new instance is created so I thought it should go in init. I implemented both init and initWithFrame (see below) but neither of these seem to be called when new instances of the subclass are created (i.e. I don’t see my NSLog messages in the console). Is there a different init method that I should use?
- (id)init {
self = [super init];
if (self) {
// Initialization code here.
NSLog(@"init");
}
return self;
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
NSLog(@"init with frame");
}
return self;
}
To answer your question, the designated initializer is initWithFrame:. However, if a view is encoded in a NIB (as it is in this case), initWithCoder: is called. You have to override that method.
Don’t use awakeFromNib; in general, it may get called more often than you expect, and I’ve seen it cause people trouble.
However, a good place to do initialization of your cell is in the table view delegate method viewForTableColumn:row: — you can still use it and use bindings.
corbin
(I wrote the class in question).