cocoa - 以编程方式将 NSTableView 添加到 NSView

标签 cocoa nstableview nsview nstableviewcell nstablecolumn

我在以编程方式将 NSTableView 添加到 NSView 时遇到了一些麻烦。该 View 是 NSSplitView 的第一个 View 。我的指针设置正确,我确信这一点,因为我可以毫无问题地将 NSButton 添加到 View 中。我的 TableView 的委托(delegate)和数据源方法也按预期工作。如果我使用界面生成器将 TableView 添加到我的 View 中,它就可以工作。但是,我不想使用IB。我希望能够通过代码来做到这一点。这是我当前正在使用的代码。

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




}

NSButton 对象出现在屏幕上,但如果我注释掉该按钮,则 TableView 不会出现。我究竟做错了什么。谢谢您的帮助。

最佳答案

谢谢你,在你的帮助下我终于弄清楚了。 IB 会自动在 TableView 周围插入 NSScrollview,并且还会为您插入一列。为了从代码中做到这一点,您需要分配一个 ScrollView 和一个列。如果其他人遇到这个问题,这就是我目前正在使用的方法。

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


}

感谢您的帮助。

关于cocoa - 以编程方式将 NSTableView 添加到 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886048/

相关文章:

cocoa - Xcode 项目更新为 3.2?

cocoa - 是否有理由不在核心数据实体中使用相同的属性名称?

swift - NSTextView 示例中带有 NSTextList 的项目符号列表?

macos - 是否可以使用 Interface Builder 设计 NSTableView 单元格? (不适用于 iOS 应用)

objective-c - Cocoa 中 AuthorizationExecuteWithPriveleges 的非常奇怪的问题

swift - 如何快速响应 macOS 应用程序中的 "Delete"命令?

objective-c - 有什么方法可以将 mouseOver 事件添加到 cocoa 中的 tableView 中吗?

cocoa - 在 NSSplitView 中设置 View 不起作用

objective-c - 相同的命令,不同的结果 setFrame : NSView

objective-c - 子类化 NSCollectionView 后的绘图问题