views:

275

answers:

3

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

A: 

It doesn't appear (from your sample) that you're putting the tableView inside the scrollView. Is it getting added as a subview somewhere you're not showing us?

Also, are you sure you need a separate scrollview? Doesn't tableView implement scrolling already?

David Gelhar
The thing is, if I don't make a separate ScrollView the TableView doesn't appear at all. And I'm pretty sure that the scrollView needs to be added separately.. Just like with textViews.. But ofcourse I could be wrong =/I've read somewhere that setting the documentView is all you need with scrollViews.. And the TableView is being shown inside the scrollView.. But no data is being presented inside of it despited the fact that numberOfRows is being called..
Jakob Dam Jensen
David Gelhar: No, NSTableView does not implement scrolling. No NSViews do, except NSScrollView—that's what NSScrollView is for.
Peter Hosey
+3  A: 

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

ericgorr
I actually like the dot syntax =)And thank you for the code
Jakob Dam Jensen
+3  A: 

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.

Peter Hosey
thank you so much... stupid stupid stupid =)
Jakob Dam Jensen