Take the 2-minute tour ×

I am making an NSTableView programmatically but for some reason no matter what I do, I cannot make the darn headerView show up. It is imperative that I do this programmatically and not use the IB because I am actually developing this widget in an IDE called clozure cl which is a lisp ide that includes a cocoa bridge. Originally I thought this problem might have been caused by my development environement but I just created an example in Xcode using only objective C and it seems that the problem persists. What I do is pretty straightforward:

I make a window in the IB and in its awkefromnib methods I create and setup a table-view here is the code:

- (void)awakeFromNib {
mydatasource *data = [[mydatasource alloc] init];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"id"];
NSTableView *table = [[NSTableView alloc] initWithFrame: [[self                      

[table setDataSource:data];
[table addTableColumn:column];
[table addTableColumn:column];
[[self contentView] addSubview:table];

Here is the code for my data source object:

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
printf("NUM ROwS");
    return 4;
 printf("THE OTHER ONE"); 
 return @"OKAY";

With this code I get a window with two colums and four rows and each cell displaying the string "OKAY", this is all fine and good except the table has no header. This might make sense except when I look at the tables header method, it has an initialized header with a frame whose values make sense. I am just wondering why I do not see it. Is there some special kind of magic I need to do so the header will display? I cannot seem to find any clues in the documentation. Once again it is imperative for the lisp ide that this be done programmatically so it would not be helpful no suggest using the IB which I know will have a working headerView. Thanks a lot.

share|improve this question

2 Answers 2

up vote 36 down vote accepted

Here is some code to programmatically create a table view with a scroll bar, and multiple columns.

// create a table view and a scroll view
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(10, 10, 380, 200)];
NSTableView * tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 364, 200)];
// create columns for our table
NSTableColumn * column1 = [[NSTableColumn alloc] initWithIdentifier:@"Col1"];
NSTableColumn * column2 = [[NSTableColumn alloc] initWithIdentifier:@"Col2"];
[column1 setWidth:252];
[column2 setWidth:198];
// generally you want to add at least one column to the table view.
[tableView addTableColumn:column1];
[tableView addTableColumn:column2];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView reloadData];
// embed the table view in the scroll view, and add the scroll view
// to our window.
[tableContainer setDocumentView:tableView];
[tableContainer setHasVerticalScroller:YES];
[[self contentView] addSubview:tableContainer];
[tableContainer release];
[tableView release];
[column1 release];
[column2 release];

Just thought I would post this for anyone still looking for a straight forward answer. :)

share|improve this answer
Thank you! I had a XIB table view that refused to fetch cells and was not looking forward to translating that into code by hand. <3 –  Kalle May 24 at 13:56
Just a note. Embedding table view into a scroll view is essential to display everything correctly including headers. –  Eonil May 25 at 23:25

Well I answered my own question and I thought this might be helpful to someone else, it seems that that the headerView will only show up if you add the tableview to a scrollview.

share|improve this answer
I think I can answer your question: NSTableHeaderView is an entirely different view (and class). It's just IB automatically throwing it at you if you drag out an NSTableView-set. I think IB should be more clear about what views come with such 'view sets'. –  11684 Mar 7 '13 at 17:06

Your Answer


