自动选择、聚焦并突出显示新的 NSOutlineView 行

发布于 2024-10-17 21:11:23 字数 1150 浏览 3 评论 0原文

这可能只是缺乏 NSOutlineView 的经验,但我看不出有什么方法可以做到这一点。我有一个带有添加按钮的 NSOutlineView (使用优秀的 PXSourceList 实现)这在我正确保存/写入/插入/删除行方面完全起作用。我不使用 NSTreeController,也不使用绑定。我使用以下代码添加实体:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList reloadData];
    for (int i = 0; i < [sourceList numberOfRows]; i++) {
        if (node == [sourceList itemAtRow:i]) {
            [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
            [sourceList editColumn:0 row:i withEvent:nil select:NO];
            break;
        }
    }
}

按下添加按钮时,将插入一个新行,如下所示:

在此处输入图像描述

如果我点击离开,然后选择该行并按 enter 进行编辑,它现在看起来像这样: 在此处输入图像描述

我的问题是:如何以编程方式获得相同的状态(焦点、选定、突出显示)第一次,让用户体验更好?

This is probably just lack of experience with the NSOutlineView but I can't see a way to do this. I have a NSOutlineView (implemented with the excellent PXSourceList) with an add button that is totally functional in the aspect that I save/write/insert/delete rows correctly. I do not use a NSTreeController, and I don't use bindings. I add the entity using the following code:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList reloadData];
    for (int i = 0; i < [sourceList numberOfRows]; i++) {
        if (node == [sourceList itemAtRow:i]) {
            [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
            [sourceList editColumn:0 row:i withEvent:nil select:NO];
            break;
        }
    }
}

When the add button is pressed, a new row is inserted like this:

enter image description here

If I click away, then select the row and press enter to edit it, it now looks like this:
enter image description here

My question is: How can I programmatically get the same state (focus, selected, highlighted) the first time, to make the user experience better?

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

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

发布评论

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

评论(1

北凤男飞 2024-10-24 21:11:23

这样的事情对我有用:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList noteNumberOfRowsChanged];
    NSInteger row = [sourceList rowForItem:node];
    [sourceList scrollRowToVisible:row];
    [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
    [sourceList editColumn:0 row:row withEvent:nil select:YES];
}

您可以使用 rowForItem: 而不是重复检查 itemAtRow:

您通常还需要使用 [sourceList rollRowToVisible:...] 以防新行不可见,并且您可以使用 noteNumberOfRowsChanged 而不是 reloadData,除非数据实际上已更改。

标准的 Mac 行为是选择新创建的项目的内容,因此请使用 select:YES

如果这没有帮助,那么您的代码中还存在其他内容,上面的代码片段无法传达...

一般来说,我真的建议在学习新课程时,您可以从头到尾阅读文档页面列表可用的方法(不推荐使用的方法除外),或者至少是可用于您尝试执行的任务的所有方法;您将更好地了解该类的功能,并且不太可能使用不适当/低效/不优雅的方法。

Something like this works for me:

- (void)addEntity:(NSNotification *)notification {
    // Create the core data representation, and add it as a child to the parent node
    UABaseNode *node = [[UAModelController defaultModelController] createBaseNode];
    [sourceList noteNumberOfRowsChanged];
    NSInteger row = [sourceList rowForItem:node];
    [sourceList scrollRowToVisible:row];
    [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
    [sourceList editColumn:0 row:row withEvent:nil select:YES];
}

You can use rowForItem: instead of repeatedly checking itemAtRow:.

You generally also want to use [sourceList scrollRowToVisible:...] in case the new row isn't visible and you can use noteNumberOfRowsChanged instead of reloadData, unless the data has actually changed.

The standard Mac behavior is to select the contents of a newly created item, so use select:YES.

If that doesn't help, there's something else going on in your code which the above snippet doesn't communicate...

In general, I'd really suggest when learning a new class that you read all the way through the documentation page listing the methods available (excepting the deprecated methods), or at the very least all the methods available for the task you're trying to perform; you'll get a much better idea of the capabilities of the class and be less likely to use an inappropriate/inefficient/inelegant method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文