大纲视图:objectValueForTableColumn:byItem:未调用
我正在尝试为新程序创建源列表,但在使用基于视图的 NSOutlineView 时遇到了一些问题。我的代码使用基于单元格的 NSOutlineView 工作得很好,所以我对发生的事情有点困惑。
这是我的委托和数据源代码:
#pragma mark -
#pragma mark NSOutlineView Delegate
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return (item == nil) ? YES : [(SourceListNode *)item groupItem];
}
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([(SourceListNode *)item groupItem]) {
return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
return [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
}
#pragma mark -
#pragma mark NSOutlineView Data Source
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? [sourceListNodes count] : [(SourceListNode *)item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : ([(SourceListNode *)item numberOfChildren] > 0);
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [sourceListNodes objectAtIndex:index] : [(SourceListNode *)item childAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [(SourceListNode *)item name];
}
当我通过调试器运行它时,它发现 outlineView:objectValueForTableColumn:byItem:
不执行。结果是没有文本的大纲视图。创建了正确的单元格,我可以选择它们并展开它们。我缺少什么?
更新:我删除了 IB 中的源列表,添加了一个新列表,并将其连接到我的控制器对象。结果更好,但标题单元格具有文本“HEADER CELL”,子单元格具有“Table View Cell”。
我再次通过调试器运行程序,这次执行了outlineView:objectValueForTableColumn:byItem:。但它没有填充单元格的文本。
然后我更新了 outlineView:viewForTableColumn:item:
如下:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *result;
if ([(SourceListNode *)item groupItem]) {
result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
[[result textField] setStringValue:[(SourceListNode *)item name]];
return result;
}
现在一切都按预期工作。
简而言之,我已经解决了我的问题。不过,我现在有另一个问题。对于基于视图的大纲视图,outlineView:objectValueForTableColumn:byItem:
的用途是什么?它执行了,但似乎没有做任何事情。
I'm trying to create a source list for a new program and I'm having a little issue using a view-based NSOutlineView. My code works fine using a cell-based NSOutlineView so I'm a little confused about what is happening.
Here is my code for the delegate and data source:
#pragma mark -
#pragma mark NSOutlineView Delegate
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return (item == nil) ? YES : [(SourceListNode *)item groupItem];
}
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([(SourceListNode *)item groupItem]) {
return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
return [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
}
#pragma mark -
#pragma mark NSOutlineView Data Source
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? [sourceListNodes count] : [(SourceListNode *)item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : ([(SourceListNode *)item numberOfChildren] > 0);
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [sourceListNodes objectAtIndex:index] : [(SourceListNode *)item childAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [(SourceListNode *)item name];
}
When I run this through the debugger it see that outlineView:objectValueForTableColumn:byItem:
does not execute. The result is an outline view with no text. The correct cells are created and I am able to select them and expand them. What am I missing?
UPDATE: I deleted my source list in IB, added a new one, and connected it to my controller object. The results were better, but the header cells had the text "HEADER CELL" and the child cells had "Table View Cell".
I ran the program through the debugger again and this time outlineView:objectValueForTableColumn:byItem:
was executed. It did not populate the text of the cell though.
I then updated outlineView:viewForTableColumn:item:
as follows:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *result;
if ([(SourceListNode *)item groupItem]) {
result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
[[result textField] setStringValue:[(SourceListNode *)item name]];
return result;
}
Now everything works as expected.
So in short, I have solved my problem. However, I now have another question. What is the purpose of outlineView:objectValueForTableColumn:byItem:
for view-base outline views? It executes, but does not appear to do anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于视图与基于单元格略有不同。
所不同的只是将“id”返回值替换为“NSView”。
将其更改
为:
假设您正在运行 10.7,并且您实际上要返回一个 NSView。
View-based is a tad different than cell-based.
All that is different is replacing the "id" return value to "NSView".
Change this:
To this:
This is given the assumption you are running 10.7, and you are actually going to return an NSView.