views:

236

answers:

2

I have an NSTableView that has a very small fixed number of rows.

When I create an NSTableView in Interface Builder, the NSTableView is contained within an NSScrollView. I have not found a way to make the table exist outside the context of a scroll view. Since the table only has a small number of rows, I don't want it to scroll. I want the table to resize based on the number of rows, and I want the bottom border immediately under the bottom of the last row.

If I set the height of the scroll view as follows, I get a vertical scroll bar:

height = (numRows * (rowHeight + intercellSpacingHeight))

If I add one pixel to that height, I don't get the scroll bar but I get an extra pixel between the bottom of the last row and the bottom border.

If I uncheck the "Show Vertical Scroller" checkbox in Interface Builder, the scroll bar does not appear but the table scrolls down one pixel when I select the last row.

Is there a way to have the table not scroll at all, and have the bottom border immediately under the last row?

Thanks.

+1  A: 

You could always extract the NSTableView from its enclosing scrollview (in code or in IB) ... You can embed the table into any container you wish, but it's up to you to maintain the table's size inside the container (and/or grow/shrink the container in response, depending n what you want to do).

Joshua Nozzi
Thanks, Joshua. I guess what I don't understand is how to extract the NSTableView from the NSScrollView. IB put it there, and I don't see a way to remove the scroll view without removing the table view. Perhaps I am missing something obvious? Thanks!
John Brayton
Layout --> Unembed Objects.
Joshua Nozzi
+1  A: 

In awakeFromNib you could write something like (untested):

NSScrollView *scrollView = [tableView superview];
NSView *container = [scrollView superview];
[[tableView retain] autorelease];
[tableView removeFromSuperview];
[scrollView removeFromSuperview];
[container addSubview:tableView];
[container setFrameSize:[tableView frame].size];

Alternatively, in Interface Builder you can extract a table view from a scroll view by changing to list view mode (Main Menu -> View -> as List). Then expand the view hierarchy until you see the table view. You can drag that out of the scroll view, but not into another view. You can just hook this up to an outlet and add it to a view programmatically.

You'll still need to update the height of the container when the number of rows change.

wbyoung
Awesome, thanks!
John Brayton