基于视图的“源列表”NSOutlineView不显示项目的图标和标题

时间:2012-08-25 16:57:16

标签: objective-c cocoa nsoutlineview

将NSOutlineView基于单元格更改为基于视图后,它不会显示文件系统树的图标和标题。这是我的代码:

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if ([[tableColumn identifier] isEqualToString:@"name"])
        return [(ImageAndTextCell *)cell setTextFieldImage:[item icon]];
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    return [((ConstructorFSEntity *)item) title];
}

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if ([item isKindOfClass:[FSEntity class]]) {
        return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
    } else {
        return [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
    }
}

我还有一个问题。如何将枚举项(文件系统项的数组)放入单元格“DataCell”,并将“HeaderCell”声明为父文件夹(组)并为其分配标题(例如,@“根文件夹”)和路径班级。因为,现在以前的基于视图的方法,仅在“HeaderCell”或“DataCell”中显示枚举项目,当我尝试为“HeaderCell”分配一些值时,应用程序崩溃。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

要显示项目的标题和图标,您只需更改此方法

即可

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item

如下:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if (![item isKindOfClass:[FSEntity class]]) {
        return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
    } else {
        NSTableCellView *cellView = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
        [(ImageAndTextCell *)cellView.textField.cell setTextFieldImage:[item icon]];
        cellView.textField.stringValue = [((FSEntity *)item) title];
        return cellView;
    }
}