7

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.

iUser
  • 1,075
  • 3
  • 20
  • 49
John Brayton
  • 107
  • 1
  • 7

2 Answers2

3

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
  • 60,946
  • 14
  • 140
  • 135
  • 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 Mar 04 '10 at 03:02
  • @JoshuaNozzi - Awesome, didn't know about that, was looking for that. Selected the scroll view and then Editor -> Unembed. Many thanks. –  Dec 28 '11 at 10:14
  • Editor -> Unembed is disabled for me when I select the scroll view in IB in Xcode 8. – Clifton Labrum Jan 22 '17 at 06:50
  • What happens if you click one more time to select the table view inside the scroll view? – Joshua Nozzi Jan 22 '17 at 15:23
1

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
  • 22,383
  • 2
  • 34
  • 40