5

将 NSOutlineView cell-based 更改为 view-based 后,它不会显示文件系统树的图标和标题。这是我的代码:

- (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”分配某个值时,应用程序崩溃。你能帮我解决这个问题吗?

4

1 回答 1

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;
    }
}
于 2012-08-26T21:25:14.360 回答