44

I am currently trying to learn a bit of Cocoa (using the book Cocoa Programming for Mac OS X). In one of the exercises, we set up a NSTableView with only one column, to act as a list of things.

What annoys me is that in Interface Builder, I could not find a way to have the (only) column always take the full width of the NSTableView. As a consequence, it always somehow looks like there are 2 columns when there is actually only one.

Any idea ?

tsimbalar
  • 5,790
  • 6
  • 37
  • 61

3 Answers3

56

This is just an IB problem that happens all the time (I'm not sure why). To solve this, simply just resize the table view to the size smaller than the 2nd column, then drag it back to the size you want and the 2nd column will disappear.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • 1
    thanks, this solves it, but somehow the "resizing" handle is still there. Is there some way to get rid of it ? UPDATE : OK I found it, just checking "Hidden" in the Header solves it ! – tsimbalar Sep 25 '11 at 13:21
  • I had the same issue, but the column would only expand to a certain width and then abruptly stop expanding. If you see this behavior, check that the "maximum" width of the table view column is set to something extremely large, say 100000. – Andrew Oct 19 '12 at 21:44
  • 3
    this is still valid in XCode 6.1, OSX 10.10. Left me in hell in the past two days. – Jerry Tian Oct 31 '14 at 08:05
  • 2
    And still not fixed, this answer is now almost 4 years old – Julian F. Weinert Apr 13 '15 at 20:53
  • 2
    hahah can't believe I answered this 5 years ago and this bug still exists in Xcode. Should honestly be a quick fix where the table view redraws itself when changing the number of columns. – TheAmateurProgrammer Oct 30 '16 at 15:23
  • omg - still valid (2017) - maybe 6 years it's not enough to fix this :( – hbk Jul 04 '17 at 09:04
  • This worked very well. Thanks. – Guru Dev Nov 24 '21 at 13:07
36

Here's how I did it in Xcode 4.6...

In IB, select the table view and go to the Attributes Inspector. Choose 'Uniform' for 'Column Sizing'. Then, select the table column and choose 'Autoresizes with Table' for 'Resizing'.

These options correspond to:

[tableView setColumnAutoresizingStyle:NSTableViewUniformColumnAutoresizingStyle];
[tableColumn setResizingMask:NSTableColumnAutoresizingMask];
sam
  • 3,399
  • 4
  • 36
  • 51
  • +1 I don't know if the accepted answer works, but this one surely does and seems more proper to me. – Tassos Nov 01 '13 at 12:01
  • 2
    You could also try [tableView sizeLastColumnToFit]; – David Douglas Jan 20 '14 at 14:59
  • The accepted answer is also necessary - sometimes (perhaps with IB upgrades?) the column ends up about 1 pixel narrower than it needs to be for AppKit to realize that it's supposed to take up the entire width of the table view. – Nicholas Riley Jun 09 '16 at 02:40
16

I had to use both steps - sam's answer + the comment by David Douglas.

[tableView  setColumnAutoresizingStyle:NSTableViewUniformColumnAutoresizingStyle];
[tableColumn setResizingMask:NSTableColumnAutoresizingMask];

 //AND 
 [tableView sizeLastColumnToFit];
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55