2009-08-24 30 views
4

有沒有人在這裏知道如何使NSOutlineView的可修改單元格?我使用蘋果公司的sampe代碼,我似乎根本無法使用它。獲取NSOutlineView行是可編輯的

我嘗試設置它,以便在NSOutlineView中的單元格上快速連續單擊兩次時,單元格變爲可編輯,以便用戶可以更新單元格內的文本。 (就像它在xcode和郵件等中一樣)。

我包含了這個控制器的其餘大部分代碼,希望有人能夠發現我做錯了什麼,這是非常令人沮喪的。我知道shouldEditTableColumn正在被調用,因爲它通過雙擊返回NSLog消息。

@implementation DisplayHierarchyController 
- (void)awakeFromNib { 
    // cache the reused icon images 
    folderImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain]; 
    [folderImage setSize:NSMakeSize(16,16)]; 
    objectImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericPreferencesIcon)] retain]; 
    [objectImage setSize:NSMakeSize(16,16)]; 
    diagramImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericEditionFileIcon)] retain]; 
    [diagramImage setSize:NSMakeSize(16,16)]; 
    // 
    // Tell the outline view to use a special type of cell 
    //NSTableColumn *tableColumn = [[outline tableColumns] objectAtIndex: 0]; 
    //ImageTextCell *imageTextCell = [[[ImageTextCell alloc] init] autorelease]; 
    //[imageTextCell setEditable:YES]; 
    //[tableColumn setDataCell:imageTextCell]; 
    // 
    [[[outline tableColumns] objectAtIndex: 0] setEditable: YES]; 
} 
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    NSLog(@"edit %@", tableColumn); 
    return YES; 
} 
- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    ImageTextCell *imageTextCell = [[[ImageTextCell alloc] init] autorelease]; 
    [imageTextCell setEditable:YES]; 
    return imageTextCell; 
} 
// Returns the object that will be displayed in the tree 
- (id)outlineView: (NSOutlineView *)outlineView child: (int)index ofItem: (id)item { 
    if(item == nil) 
     return [[document children] objectAtIndex: index]; 
    if([item isKindOfClass: [Item class]]) 
     return [[item children] objectAtIndex: index]; 
    return document; 
} 
- (BOOL)outlineView: (NSOutlineView *)outlineView isItemExpandable: (id)item { 
if([item isKindOfClass: [Item class]]) 
    return [[item children] count]>0; 
return NO; 
} 
- (int)outlineView: (NSOutlineView *)outlineView numberOfChildrenOfItem: (id)item { 
    if(item == nil) 
     return document.children.count; 
    if([item isKindOfClass: [Item class]]) 
     return [[item children] count]; 
    return 0; 
} 
- (id)outlineView: (NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    if([item isKindOfClass: [Item class]]) 
     return [item name]; 
    return @"n/a"; 
} 
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    NSLog(@"setObjectValue called"); 
} 
- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    [cell setEditable: YES]; 
    [cell setAllowsEditingTextAttributes: YES]; 
    [(ImageTextCell*)cell setImage: objectImage]; 
} 
- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor { 
    return YES; 
} 
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor { 
    if ([[fieldEditor string] length] == 0) { 
     // don't allow empty node names 
     return NO; 
    } else { 
     return YES; 
    } 
} 
@end 
+1

「我似乎根本無法工作。」這是什麼意思?請編輯您的問題以更具體。 – 2009-08-24 23:23:12

+0

希望能讓它更清晰。我想雙擊一行以啓用對該行的編輯 – Jacob 2009-08-25 10:47:24

+2

我正在做更多的研究,以瞭解如何使其發揮作用,蘋果文檔稱「大綱視圖繼承了其父類NSTableView的大部分行爲.... ..或更多信息,請參閱Table View Programming Guide。「一個NSTableView確實有這種方法「(BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row :(NSInteger)rowIndex」但我似乎不能夠以它獲得的方式將它添加到我的控制器調用。 – Jacob 2009-08-25 12:11:51

回答

3

我剛剛發現我可以通過更改shouldEditTableColumn「僞造」它。它真的不理想,但它的工作原理。經過這麼多小時試圖讓它起作用,至少這是一件事:

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    NSLog(@"edit %@", tableColumn); 
    [outline editColumn:0 row:[outline selectedRow] withEvent:[NSApp currentEvent] select:YES]; 
    return YES; 
} 
+0

這在技術上並不是正確答案,但它是我能找到的最接近的東西。它不是很正確,但它比任何事情都好! – Jacob 2009-08-30 01:54:33

6

列本身是否設置爲可編輯?通常情況下,你會在IB中這樣做。

此外,您是否已在您的數據源中實施the outlineView:setObjectValue: method

+1

當我點擊進入界面生成器中的單元格時,它被設置爲可編輯的。但是,我用覆蓋圖像的自定義單元覆蓋單元格,不知道是否與之相關。在setObjectValue(它不清楚從文檔):( – Jacob 2009-08-24 15:06:25

+3

我沒有說細胞,我說的專欄。它有它自己的可編輯複選框。至於'setObjectValue:':這就是你將用戶做出的改變並應用於你的模型的地方。這是'objectValue:'的相反方向,它檢查模型並將該信息提供給大綱視圖以顯示給用戶。 – 2009-08-24 22:03:04

+1

Doh,對不起,我誤解了你寫的內容。 NSTableColumn被勾選爲可編輯的(看起來這將是默認值,因爲我沒有勾選它) – Jacob 2009-08-24 23:16:19

1

我找到了解決辦法。在IB中設置列的數據單元格(編程方式在awakeFromNib中也應該工作)。我實際上使用2個不同的自定義單元類。我的解決辦法:

NSCell *cell = [tableColumn dataCellForRow: [outlineView rowForItem: item]]; 

if ([item isKindOfClass: [NSString class]]) 
    return [[[ShadowTextCell alloc] initTextCell: [cell stringValue]] autorelease]; 
return cell; 
8

我知道這是一個非常古老的職位,但如果任何一個遇到同樣的問題,這可能不涉及代碼的問題。對我而言,這是一個與XIB本身的值設置有關的問題。

因此可以說你已經複製了所有的Apple代碼,並且你已經啓動並運行了NSOutlineView,還有一些它仍然不可編輯,請去你的XIB並設置單元格的NSTextField的下列設置你想要可編輯。在我的情況下,行爲設置默認設置爲。也許它同樣的問題,你

enter image description here

乾杯。

+0

謝謝!這也是我也遇到的問題。我非常專注於修復我的代碼中的一個'錯誤',而我並沒有想過去檢查IB。 – sapi 2014-02-28 22:30:30

+0

這仍然是Xcode8和Swift 3中基於視圖的outlineViews,其中shouldEditTableColumn不會被調用。接得好。 – 2017-01-08 23:05:04