开发者

How to add a scrollable NSTableView programmatically

开发者 https://www.devze.com 2022-12-22 14:18 出处:网络
I\'m trying to add a tableview to a view in code instead of using Interface Builder and unfortunately it\'s causing some problems =(

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: scrol开发者_运维问答lView];

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


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


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消