Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a scrollable NSTableView programmatically

I'm trying to add a tableview to a view in code instead of using Interface Builder and unfortunately it's causing some problems =(

Here's an example of how I'm doing it now.

NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:someRect];
NSTableView *tableView = [[NSTableView alloc] initWithFrame: scrollView.bounds];
resultsTableView.dataSource = self;

resultsScrollView.documentView = tableView;

[someView addSubview: scrollView];

So basically I'm just putting the tableView inside the scrollView (because that's what IB is doing) and then adding the latter as a subview of the someView. The result is that a tableView appears - but the no data is shown within the tableView. Debugging shows that the dataSource is being asked for how many rows are in the tableView but the method:

tableView:objectValueForTableColumn:row:

is never being called. I suspect that this is because of my way of creating the tableView.

I've tried google but no luck and the "Introduction to Table Views Programming Guide" on macdevcenter wasn't helpful either. What am I missing?

Thanks in advance...

like image 279
Jakob Dam Jensen Avatar asked Mar 12 '10 11:03

Jakob Dam Jensen


2 Answers

I created the default sample project and then did the following:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
    NSRect          scrollFrame = NSMakeRect( 10, 10, 300, 300 );
    NSScrollView*   scrollView  = [[[NSScrollView alloc] initWithFrame:scrollFrame] autorelease];

    [scrollView setBorderType:NSBezelBorder];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:YES];
    [scrollView setAutohidesScrollers:NO];

    NSRect          clipViewBounds  = [[scrollView contentView] bounds];
    NSTableView*    tableView       = [[[NSTableView alloc] initWithFrame:clipViewBounds] autorelease];

    NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn headerCell] setStringValue:@"First Column"];
    [tableView  addTableColumn:firstColumn];

    NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];
    [[secondColumn headerCell] setStringValue:@"Second Column"];
    [tableView  addTableColumn:secondColumn];

    [tableView setDataSource:self];
    [scrollView setDocumentView:tableView];

    [[[self window] contentView] addSubview:scrollView];

}



- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return 100;
}


- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    NSString* cellValue = [NSString stringWithFormat:@"%@ %ld", [aTableColumn identifier], (long)rowIndex];

    return cellValue;
}

You can grab the sample app TableViewInCode

(Oh, to the objective-c dot syntax, just say no. :-)

like image 126
ericg Avatar answered Oct 05 '22 23:10

ericg


The result is that a tableView appears - but the no data is shown within the tableView. Debugging shows that the dataSource is being asked for how many rows are in the tableView but the method:

tableView:objectValueForTableColumn:row:

is never being called.

It's not asking you for the object values for any columns because it doesn't have any columns. Create some NSTableColumn instances and add them to the table view.

like image 24
Peter Hosey Avatar answered Oct 06 '22 00:10

Peter Hosey