Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed Height NSTableView, Avoid Scrolling

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.

like image 780
John Brayton Avatar asked Mar 04 '10 02:03

John Brayton


2 Answers

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).

like image 187
Joshua Nozzi Avatar answered Nov 05 '22 20:11

Joshua Nozzi


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.

like image 36
wbyoung Avatar answered Nov 05 '22 20:11

wbyoung