使 NSOutlineView 行可编辑

发布于 2024-08-02 15:31:06 字数 3490 浏览 8 评论 0原文

这里有人知道如何使 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

Does anyone here know how to make cells in NSOutlineView's editible? Im using the sampe code from apple and I cant seem to get it work at all.

I am trying to set it up so that when you click twice in rapid succession on a cell in the NSOutlineView, the cell becomes editible so the user can update the text inside the cell. (In the same way as it works in xcode, and mail and so on).

I am including most of the rest of the code of this controller in the vain hope someone can spot what I am doing wrong, this is very frustrating. I know shouldEditTableColumn is being called as it is returning the NSLog message upon double click.

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枕头说它不想醒 2024-08-09 15:31:06

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

假设您已经复制了所有 Apple 代码,并且您的 NSOutlineView 已启动并运行,但它仍然不可编辑,请转到您的 XIB 并设置您想要的单元格的 NSTextField 的以下设置可编辑。就我而言,行为设置默认设置为。也许你也有同样的问题

在此处输入图像描述

干杯。

I know this is a very old post, but if any one is experiencing the same issue, this may not be an issue related to code. For my case it was an issue related to do with a value set in the XIB itself.

So lets say you've copied all the Apple code, and you've got your NSOutlineView up and running, and some how its still not editable, go to your XIB and set the following setting of the NSTextField of the cell you want to be editable. In my case the behavior setting was set to none by default. Maybe its the same problem for you

enter image description here

Cheers.

山色无中 2024-08-09 15:31:06

该列本身是否设置为可编辑?通常,您会在 IB 中执行此操作。

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

Is the column itself set as editable? Ordinarily, you would do this in IB.

Also, have you implemented the outlineView:setObjectValue: method in your data source?

少年亿悲伤 2024-08-09 15:31:06

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

Ive just discovered I can "fake it" by altering the shouldEditTableColumn. Its really not ideal, but it works. After so many hours trying to get it to work, at least this is something:

- (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;
}
没企图 2024-08-09 15:31:06

我找到了解决这个问题的方法。在 IB 中设置列​​的数据单元格(在 awakeFromNib 中以编程方式也应该可以)。我实际上使用了 2 个不同的自定义单元类。我的解决方案:

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

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

I found a way around this. Set the data cell for the column in IB (programmatically in awakeFromNib should work too). I actually use 2 different custom cell classes. My solution:

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

if ([item isKindOfClass: [NSString class]])
    return [[[ShadowTextCell alloc] initTextCell: [cell stringValue]] autorelease];
return cell;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文