2012-08-25 83 views
5

將基於單元格的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」時,應用程序崩潰。你能幫助我嗎?

回答

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; 
    } 
} 
+3

'NSTabelCellView'因此,你已經提供一個屬性'imageView'可以通過設置'cellView.imageView.objectValue = [item icon]'來設置圖像。您不需要SourceView示例中的ImageAndTextCell類。 – Stephan

相關問題