【问题标题】:Autoselect, focus and highlight a new NSOutlineView row自动选择、聚焦和突出显示一个新的 NSOutlineView 行
【发布时间】:2011-02-18 22:56:11
【问题描述】:

这可能只是缺乏使用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 进行编辑,它现在看起来像这样:

我的问题是:我怎样才能以编程方式第一次获得相同的状态(焦点、选中、突出显示),以使用户体验更好?

【问题讨论】:

    标签: cocoa nstableview nsoutlineview selectedindex pxsourcelist


    【解决方案1】:

    这样的东西对我有用:

    - (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 scrollRowToVisible:...] 以防新行不可见,并且您可以使用noteNumberOfRowsChanged 而不是reloadData,除非数据实际上已更改。

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

    如果这没有帮助,那么上面的 sn-p 没有传达您的代码中的其他内容...

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多