Question

So I'm trying to create a custom NSTableView by subclassing NSTableCellView and a NSTextField inside of the cellview. I'm trying to write some init code in the initWithFrame: method for the NSTextField subclass (TableTextField), but it looks like the initWithFrame: method isn't being called at all, even when I create new rows for the NSTableView, which each contain an instance of the TableTextField. Here's the code in the TableTextField.m file:

#import "TableTextField.h"

@implementation TableTextField

- (id)initWithFrame:(NSRect)frame//This isn't even being called.
{
    self = [super initWithFrame:frame];
    if (self) {
        self.ad = [[NSApplication sharedApplication] delegate];
    }
    return self;
}

- (void)mouseDown:(NSEvent*) theEvent{
    NSLog(@"A");//TEST
    self.ad = [[NSApplication sharedApplication] delegate];//This works. Why isn't it happening in init?
    [self.ad.classViewController addHomeworkItem];
}

@end

I've been able to solve the problem by just putting the code i need in the mouseDown: method, which is really the only thing I need, but I feel like it's bad practice to redeclare self.ad every time i need to use it, rather than just declaring it once and accessing it for each use, and I can't seem to figure out why initWithFrame: isn't being called. I'm assuming it has to do with the way objects inside NSTableCellViews are initialized, but I haven't found a real explanation. Any suggestions on how to solve this, or explanations as to why it's happening would be welcome. Thanks!

Here's an image of how the NSTableView is set up (with 3 rows):

Was it helpful?

Solution

If you are using XIB files to layout the views, the initialiser method being called is initWithCoder:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top