【发布时间】:2023-04-04 17:48:01
【问题描述】:
我有一个大纲视图委托,并且正在覆盖 outlineView:dataCellForTableColumn:item: 以使我的大纲视图中的单元格成为按钮(请参阅this question)。这是我的代表的代码:
- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;
{
MyCell * myCell = [[MyCell alloc] init];
// return nil;
return myCell;
}
这样做有一个奇怪的副作用。在我的大纲视图的数据源中,outlineView:objectValueForTableColumn:byItem: 方法总是获取 tableColumn 的空值。
代码是:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
printf("tableColumn:%s\ttable identifier: %s\n", [[tableColumn className] cString], [[tableColumn identifier] cString]);
return [item valueForKey:[tableColumn identifier]];
}
输出是:
tableColumn:(null) table identifier: (null)
奇怪的是,这只发生在我实现 outlineView:dataCellForTableColumn:item: 方法时。我在这里错过了什么?
编辑:
像这样修改委托函数似乎可以解决问题:
- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;
{
printf("delegate column identifier: %s\n", [[tableColumn identifier] cStringUsingEncoding:NSASCIIStringEncoding]);
if (tableColumn == nil)
{
return nil;
}
MyCell * aCustomCell = [[MyCell alloc] init];
return aCustomCell;
}
但是,我真的不明白这里发生了什么。如果有人可以解释,那将很有帮助。谢谢!
【问题讨论】:
标签: cocoa