What is the designated initializer for subclasses of NSTableCellView?
I created a subclass NSTableCellView
to create a custom drawing. The contents of the table are obtained by binding to an NSArrayController, so new instances of my NSTableCellView subclass are "automatically" created when new data is 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've implemented both init and initWithFrame (see below), but none of them seem to get called when new instances of the subclass are created (i.e. I don't see the NSLog message in the console). Is there any other init method I should be using?
- (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;
}
source to share
To answer your question, the designated initializer is initWithFrame :. However, if the view is NIB encoded (as in this case), initWithCoder: is called. You must override this method.
Don't use awakeFromNib; in general, it can be called more often than you would expect, and I saw that it made people worry.
However, a good place to initialize your cell is in the viewTableColumn: row: delegate method - you can still use that and use bindings.
Corbin (I wrote the class in question).
source to share