tags:

views:

390

answers:

2

I have a custom NSTableView subclass which is bound to a data source (an NSArray) which updates asynchronously. When items are added to the array, rows are automatically added to the tableview. Awesome!

My question is this: How can I detect that this magic has happened so that I can perform some other tasks related to the display of my custom tableview? Is there a method that I can override in my subclass which will be called when the tableview is updated?

+4  A: 

You don't need to subclass NSTableView to change its height based on the number of rows. In your controller, just monitor the data array using KVO and adjust the frame size of the tableview's scrollview (you can find it using enclosingScrollView) when rows are added or removed. I've used this technique before and it's worked well. You can use the tableview's rowHeight and intercellSpacing methods to calculate the height of the frame.

Marc Charbonneau
Excellent! Thank you. That's what I've been looking for :)
e.James
As a late follow-up, I just wanted to mention that this solution worked out perfectly. Cocoa makes things pretty simple with KVC/KVO. Thanks again.
e.James
+2  A: 

Looked high and low for days on this solution. It worked like a charm, thanks! Here's a sample of my code for others to follow:


// tv = NSTableView
// view = NSView

int height = ([tv rowHeight] + [tv intercellSpacing].height)  * [itemNodes count];

NSScrollView *sv = [tv enclosingScrollView];

NSRect svFrame = [sv frame];
svFrame.size.height = height;
[sv setFrame:svFrame];

NSRect viewFrame = [view frame];
viewFrame.size.height = height;
[view setFrame:viewFrame];