How to add a scrollable NSTableView programmatically

I am trying to add a table view to a view in code instead of using Interface Builder and unfortunately this causes some problems = (

Here is an example of how I am 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 just put the tableView inside the scrollView (because that's what IB does) and then adds the latter as a subview of someView. As a result, a tableView appears, but the data is not displayed in the tableView. Debugging shows that the number of rows in the View table is set in the dataSource, but the method:

tableView:objectValueForTableColumn:row:

never called. I suspect this is due to my way of creating a tableView.

I tried google, but no luck, and the "Programming Guide in Table Presentation" also did not help in the "Madevcenter". What am I missing?

...

+5
3

tableView, View . , dataSource View, :

tableView:objectValueForTableColumn:row:

.

, . NSTableColumn .

+7

, :

- (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;
}

TableViewInCode

(, objective-c, "": -)

+16

( ), tableView scrollView. , ?

Also, are you sure you need a separate scroll? Does scrolling tableView not work already?

0
source

All Articles