NSOutlineView源列表的数据

时间:2011-11-27 14:31:17

标签: objective-c cocoa nsoutlineview

我有一个源列表的委托类。但是我不知道outlineView的返回变量是什么类型:objectValueForTableColumn:byItem:应该是。

在片刻,我的代码看起来像这样,所有结构都可以工作,但没有显示文字:

@interface DataSource : NSObject<NSOutlineViewDelegate,NSOutlineViewDataSource>

@end

而.m

@implementation DataSource
// Data Source methods

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {

    return (item == nil) ? 1 : [item numberOfChildren];
}


- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return (item == nil) ? YES : ([item numberOfChildren] != -1);
}


- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {

    return (item == nil) ? [FileSystemItem rootItem] : [(FileSystemItem *)item childAtIndex:index];
}

//-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    return @"Some String";
}


@end

我制作了一个示例应用来展示差异。 Image is here

1 个答案:

答案 0 :(得分:7)

我想你有基于视图的NSTableView。在委派中,您应该实现方法- (id)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn。它可能看起来像这样:

- (id)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item{
    if ([[item representedObject] parent] == nil) {
        return [ov makeViewWithIdentifier:@"HeaderCell" owner:self];
    }else{
        return [ov makeViewWithIdentifier:@"DataCell" owner:self];
    }
}

HeaderCellDataCell是表格单元格视图的默认标识符。