On 26/06/2009, at 11:54 AM, Keary Suska wrote:


On Jun 25, 2009, at 7:33 PM, Peter Zegelin wrote:

I have come across what seems to be a weird situation and can't really think of a good solution. I'm relatively new to Cocoa so its probably just a simple misunderstanding.

I have a subclass of NSViewController that also acts as the delegate for the NSTableView that it controls. This tableview is loaded into my main window. I want to be able to hide some columns in the tableview and as a result, the number of rows in the tableview will vary according to numColumns. ie:

-(int)numberOfRowsInTableView:(NSTableView *)tableView{
        
        return myDataSize/numColumns;
}

Now, I haven't got around to actually varying the number of columns in the table yet, but I do have a property 'numColumns' that I set to a reasonable value in awakeFromNib:

- (void)awakeFromNib{
        
        numColumns = 8;
}


Unfortunately my numberOfRowsInTableView is being called 'before' awakeFromNib, so initially numColumns = 0 and I get an error (myDataSize/numColumns will be infinite).

I always thought awakeFromNib would get called first. But in this case not so. So can anyone suggest why, and a workaround?

There is no such guarantee for -awakeFromNib. You either need to set the value earlier, or call -reloadData on the table.


Well I guess the question then becomes 'where is earlier'!

I tried overloading NSViewController::loadView and it also gets called 'after' the first call to numberOfRowsInTableView. If I have the 3 methods awakeFromNib, numberOfRowsInTableView and loadView this is the calling sequence I get:

numberOfRowsInTableView
awakeFromNib
loadView
numberOfRowsInTableView
numberOfRowsInTableView
objectValueForTableColumn  <- drawing will start here I guess

Grahams suggestion is a good workaround (thanks!) but I still don't quite get it.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to