使NSOutlineView行可编辑

时间:2009-08-24 10:22:25

标签: objective-c cocoa nsoutlineview

有没有人知道如何在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

4 个答案:

答案 0 :(得分:11)

我知道这是一篇非常古老的帖子,但如果有人遇到同样的问题,这可能不是与代码相关的问题。对于我的情况,这是一个与XIB本身的值设置有关的问题。

因此,假设您已经复制了所有Apple代码,并且您已经启动并运行了NSOutlineView,以及一些仍然无法编辑的内容,请转到您的XIB并设置以下单元格的NSTextField设置想要可编辑。在我的情况下,行为设置默认设置为 none 。对你来说也许是同样的问题

enter image description here

干杯。

答案 1 :(得分:6)

列本身是否设置为可编辑?通常,你会在IB中这样做。

另外,您是否在数据源中实施了the outlineView:setObjectValue: method

答案 2 :(得分:2)

我刚刚发现我可以通过改变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;
}

答案 3 :(得分: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;