Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On 26/06/2009, at 1:24 PM, Quincey Morris wrote: After all, the non-band-aid way is almost as easy: - (id) initWithNibName: (NSString*) nibNameOrNil bundle: (NSBundle*) nibBundleOrNil { self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (!self) return nil; numColumns = 8; return self; } (I'm assuming that the view controller is not itself if a nib file. If it is, you need to used initWithCoder instead.) This is definitely the right answer this time. If it isn't the wrong answer. Heh :-) Ok - I've implemented initWithNibName and set the value there. Works great - thanks! As an aside, this caused another little problem because I then immediately called my method to show and hide columns, but at that point there was no ref to the tableview. I ended up also having: - (void)awakeFromNib{ [self setColumnHidden]; <- tried this in initWithNibName but it was too early } which fixes that as well. thanks everyone, Peter ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On 26/06/2009, at 1:24 PM, Quincey Morris wrote: After all, the non-band-aid way is almost as easy: I wholeheartedly agree - this was me just not taking the trouble to understand the real issue. However, in my defence I'd say that any time you perform a division by a value that something else can change, protecting yourself against silly values (in this case 0) is usually wise unless you can absolutely guarantee that the value is always sensible. --Graham ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On Jun 25, 2009, at 20:02, Peter Zegelin wrote: Go easy on me here - but I just have a regular NSTableView - no subclass. The NSViewController - which is subclassed -is the delegate and handles numberOfRowsInTableView/ objectValueForTableColumn. So why didn't my question make sense? Eek! I confused myself. What I said would have been the right answer if it wasn't the wrong answer. Actually, the answer is pretty much the same -- you should set a valid number of columns in the view controller's initializer, not in its awakeFromNib. That means you're actually going to have to give it an initializer if it doesn't have one already. The only problem with Graham's band-aid approach is that now you've baked the concept of "numColumns == 0 means the view controller hasn't been initialized yet" into your implementation. What if, in the future, something else that references numColumns gets called before awakeFromNib? After all, the non-band-aid way is almost as easy: - (id) initWithNibName: (NSString*) nibNameOrNil bundle: (NSBundle*) nibBundleOrNil { self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (!self) return nil; numColumns = 8; return self; } (I'm assuming that the view controller is not itself if a nib file. If it is, you need to used initWithCoder instead.) This is definitely the right answer this time. If it isn't the wrong answer. ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On 26/06/2009, at 12:40 PM, Quincey Morris wrote: On Jun 25, 2009, at 19:14, Peter Zegelin wrote: 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. Your situation requires a little care because you (presumably, otherwise your question wouldn't make sense) have a NSTableView subclass, Go easy on me here - but I just have a regular NSTableView - no subclass. The NSViewController - which is subclassed -is the delegate and handles numberOfRowsInTableView/ objectValueForTableColumn. So why didn't my question make sense? (Alternatively, follow Graham's approach, which in effect makes initialization unnecessary.) Yes this works fine. Peter ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On Jun 25, 2009, at 19:14, Peter Zegelin wrote: 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. Specifically, the first thing that's going be called is either initWithCoder: or init: http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#/ /apple_ref/doc/uid/1051i-CH4-SW18 After that, the object is potentially available to other objects -- and itself, of course -- before its awakeFromNib is called. As Keary said, there's no basis for believing that awakeFromNib is the *first* method that can possibly be called on a just-unarchived object. Your situation requires a little care because you (presumably, otherwise your question wouldn't make sense) have a NSTableView subclass, but what's archived is actually a NSTableView (according to the above document, if you dragged a table view into your XIB file in IB and then changed its class). It gets transmogrified into your subclass as/after it's unarchived (it says), so you'd better set your initial state in initWithCoder. If you think about it, your NSTableView subclass never finished its initialization before it was archived, and if you create one programatically it will fail in exactly the same way as when it's being loaded from the nib, for the same reason. (So you'll have to implement its designated initializer too, if you might ever create one programatically.) (Alternatively, follow Graham's approach, which in effect makes initialization unnecessary.) If you really want to know what's calling 'numberOfRowsInTableView' early, set a breakpoint there and look. Could well be the table view itself. ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On 26/06/2009, at 11:55 AM, Kyle Sluder wrote: On Thu, Jun 25, 2009 at 6:33 PM, Peter Zegelin wrote: Unfortunately my numberOfRowsInTableView is being called 'before' awakeFromNib, so initially numColumns = 0 and I get an error (myDataSize/numColumns will be infinite). So set a flag in -awakeFromNib. Check this flag in -numberOfRowsInTableView: and return 0 if this flag isn't set. I always thought awakeFromNib would get called first. But in this case not so. So can anyone suggest why, and a workaround? The order of -awakeFromNib calls is not guaranteed. In fact, you probably have an outlet from your tree controller to your table view, so the table view must necessarily be awoken first, since all outlets are guaranteed to be set up before -awakeFromNib is called. NSTableView's -awakeFromNib probably causes it to probe its delegate. --Kyle Sluder Yes, that makes sense - there is an outlet. At least I know its not my code. Thanks! Peter ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On Thu, Jun 25, 2009 at 6:33 PM, Peter Zegelin wrote: > Unfortunately my numberOfRowsInTableView is being called 'before' > awakeFromNib, so initially numColumns = 0 and I get an error > (myDataSize/numColumns will be infinite). So set a flag in -awakeFromNib. Check this flag in -numberOfRowsInTableView: and return 0 if this flag isn't set. > I always thought awakeFromNib would get called first. But in this case not > so. So can anyone suggest why, and a workaround? The order of -awakeFromNib calls is not guaranteed. In fact, you probably have an outlet from your tree controller to your table view, so the table view must necessarily be awoken first, since all outlets are guaranteed to be set up before -awakeFromNib is called. NSTableView's -awakeFromNib probably causes it to probe its delegate. --Kyle Sluder ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
On 26/06/2009, at 11:33 AM, Peter Zegelin wrote: I always thought awakeFromNib would get called first. But in this case not so. So can anyone suggest why, and a workaround? This sounds a bit odd, and I can't say why. But I can offer a workaround: -(int)numberOfRowsInTableView:(NSTableView *)tableView{ if( numColumns == 0 ) return myDataSize / 8; else return myDataSize/numColumns; } ;-) --Graham ___ 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
Re: NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
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. Best, Keary Suska Esoteritech, Inc. "Demystifying technology for your home or business" ___ 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
NSViewController awakeFromNib and NSTableView numberOfRowsInTableView
Hello, 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? thanks! Peter ___ 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