macos 以编程方式创建 NSTableView(无法显示 NSHeaderView)(cocoa osx)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4599747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically creating an NSTableView (trouble getting the NSHeaderView to show up)(cocoa osx)
提问by Mike2012
I am making an NSTableView programmatically but for some reason no matter what I do, I cannot make the darn headerView show up. It is imperative that I do this programmatically and not use the IB because I am actually developing this widget in an IDE called clozure cl which is a lisp ide that includes a cocoa bridge. Originally I thought this problem might have been caused by my development environement but I just created an example in Xcode using only objective C and it seems that the problem persists. What I do is pretty straightforward:
我正在以编程方式制作 NSTableView,但出于某种原因,无论我做什么,我都无法显示该死的 headerView。我必须以编程方式执行此操作而不使用 IB,因为我实际上是在名为 clozure cl 的 IDE 中开发此小部件,该 IDE 是一个包含可可桥的 lisp IDE。最初我认为这个问题可能是由我的开发环境引起的,但我刚刚在 Xcode 中仅使用目标 C 创建了一个示例,似乎问题仍然存在。我所做的非常简单:
I make a window in the IB and in its awkefromnib methods I create and setup a table-view here is the code:
我在 IB 中创建了一个窗口,并在其 awkefromnib 方法中创建并设置了一个表视图,这里是代码:
- (void)awakeFromNib {
mydatasource *data = [[mydatasource alloc] init];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"id"];
NSTableView *table = [[NSTableView alloc] initWithFrame: [[self
contentView]frame]];
[table setDataSource:data];
[table addTableColumn:column];
[table addTableColumn:column];
[[self contentView] addSubview:table];
}
Here is the code for my data source object:
这是我的数据源对象的代码:
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
printf("NUM ROwS");
return 4;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
printf("THE OTHER ONE");
return @"OKAY";
}
With this code I get a window with two colums and four rows and each cell displaying the string "OKAY", this is all fine and good except the table has no header. This might make sense except when I look at the tables header method, it has an initialized header with a frame whose values make sense. I am just wondering why I do not see it. Is there some special kind of magic I need to do so the header will display? I cannot seem to find any clues in the documentation. Once again it is imperative for the lisp ide that this be done programmatically so it would not be helpful no suggest using the IB which I know will have a working headerView. Thanks a lot.
使用此代码,我得到一个带有两列和四行的窗口,每个单元格显示字符串“OKAY”,这一切都很好,只是表格没有标题。这可能是有意义的,除非当我查看表头方法时,它有一个初始化的头,带有一个其值有意义的框架。我只是想知道为什么我看不到它。我是否需要使用某种特殊的魔法来显示标题?我似乎在文档中找不到任何线索。再一次,lisp ide 必须以编程方式完成此操作,因此如果不建议使用我知道将具有可用 headerView 的 IB,这将无济于事。非常感谢。
回答by Alex Nichol
Here is some code to programmatically create a table view with a scroll bar, and multiple columns.
下面是一些代码,用于以编程方式创建带有滚动条和多列的表格视图。
// create a table view and a scroll view
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(10, 10, 380, 200)];
NSTableView * tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 364, 200)];
// create columns for our table
NSTableColumn * column1 = [[NSTableColumn alloc] initWithIdentifier:@"Col1"];
NSTableColumn * column2 = [[NSTableColumn alloc] initWithIdentifier:@"Col2"];
[column1 setWidth:252];
[column2 setWidth:198];
// generally you want to add at least one column to the table view.
[tableView addTableColumn:column1];
[tableView addTableColumn:column2];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView reloadData];
// embed the table view in the scroll view, and add the scroll view
// to our window.
[tableContainer setDocumentView:tableView];
[tableContainer setHasVerticalScroller:YES];
[[self contentView] addSubview:tableContainer];
[tableContainer release];
[tableView release];
[column1 release];
[column2 release];
Just thought I would post this for anyone still looking for a straight forward answer. :)
只是想我会为任何仍在寻找直接答案的人发布此信息。:)
回答by Bjorn
I wrote this Swift version of @Alex Nichol's answer:
我写了@Alex Nichol的答案的这个Swift版本:
let tableContainer = NSScrollView(frame:NSMakeRect(10, 10, 380, 200))
let tableView = NSTableView(frame:NSMakeRect(0, 0, 364, 200))
let column1 = NSTableColumn(identifier: "Col1")
let column2 = NSTableColumn(identifier: "Col2")
column1.width = 252
column2.width = 198
tableView.addTableColumn(column1)
tableView.addTableColumn(column2)
tableView.setDelegate(self)
tableView.setDataSource(self)
tableView.reloadData()
tableContainer.documentView = tableView
tableContainer.hasVerticalScroller = true
window.contentView.addSubview(tableContainer)
It worked.
有效。
回答by Mike2012
Well I answered my own question and I thought this might be helpful to someone else, it seems that that the headerView will only show up if you add the tableview to a scrollview.
好吧,我回答了我自己的问题,我认为这可能对其他人有帮助,似乎只有将 tableview 添加到滚动视图时,headerView 才会显示。