1

I am having a bit of trouble adding a NSTableView to an NSView programatically. The view is the first view of an NSSplitView. My Pointers are set up right i am sure of it because I can add a NSButton to the view no problem. Also my tableview's delegate and datasource methods are working as expected. If I use interface builder to add the table view to my view it works. However, I dont want to use IB. I would like to be able to do this through code. Here is the code I am currently using.

-(void)awakeFromNib{


    tableData = [[NSMutableArray alloc]initWithObjects:@"March",@"April",@"May", nil];


    tableView = [[NSTableView alloc]initWithFrame:firstView.frame];



    [tableView setDataSource:self];
    [tableView setDelegate:self];



    [firstView addSubview:tableView];

    NSButton *j = [[NSButton alloc]initWithFrame:firstView.frame];
    [j setTitle:@"help"];

    [firstView addSubview:j];




}

The NSButton object appears on screen although if I comment out the button the tableview does not appear. What am I doing wrong. Thanks for the help.

4

2 回答 2

5

谢谢,在您的帮助下,我能够解决这个问题。IB 自动在表格视图周围插入 NSScrollview,它还为您插入一列。为了从代码中执行此操作,您需要分配一个滚动视图和一个列。如果其他人遇到这个问题,这就是我目前正在使用的。

-(void)awakeFromNib{

    tableData = [[NSMutableArray alloc]initWithObjects:@"March",@"April",@"May", nil];

    NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:firstView.bounds];

    //This allows the view to be resized by the view holding it 
    [tableContainer setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    tableView = [[NSTableView alloc] initWithFrame:tableContainer.frame];
    [tableView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    NSTableColumn *column =[[NSTableColumn alloc]initWithIdentifier:@"1"];
    [column.headerCell setTitle:@"Header Title"];


    [tableView addTableColumn:column];



    [tableView setDataSource:self];
    [tableView setDelegate:self];


    [tableContainer setDocumentView:tableView];

    [firstView addSubview:tableContainer];

    //You mentioned that ARC is turned off so You need to release these:
    [tableView release];
    [tableContainer release];
    [column release];


}

谢谢你的帮助。

于 2012-08-09T19:29:34.870 回答
1

NSTableView默认情况下在NSScrollView. 所以你可以这样做:

tableData = [[NSMutableArray alloc] initWithObjects:@"March",@"April",@"May", nil];

NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:firstView.frame];
tableView = [[NSTableView alloc] initWithFrame:firstView.frame];

[tableView setDataSource:self];
[tableView setDelegate:self];

[tableContainer setDocumentView:tableView];

[firstView addSubview:tableContainer];

//You mentioned that ARC is turned off so You need to release these:
[tableView release];
[tableContainer release];
于 2012-08-09T16:14:05.023 回答