使NSOutlineView行可编辑

Jac*_*cob 4 cocoa objective-c 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
Run Code Online (Sandbox Code Playgroud)

iOS*_*com 11

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

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

在此输入图像描述

干杯.


Pet*_*sey 6

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

另外,您是否在数据源中实现了该outlineView:setObjectValue:方法

  • 我没有说过细胞,我说的是柱子.它有自己的可编辑复选框.至于`setObjectValue:`:这是你进行用户更改并将其应用于模型的地方.这是`objectValue:`的反方向,它检查模型并将该信息提供给大纲视图以向用户显示. (3认同)