gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:30:20 24 4
gpt4 key购买 nike

我在以编程方式将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com