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

时间:2011-02-18 22:56:11

标签: cocoa nstableview nsoutlineview selectedindex pxsourcelist

这可能只是缺乏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 image description here

如果我点击了,然后选择该行并按enter进行编辑,它现在看起来像这样: enter image description here

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

1 个答案:

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

如果这没有帮助,您的代码中还会发生其他内容,而上述代码段无法通信......

一般来说,我真的建议你在学习一个新课程的时候,你会在文档页面中一直列出可用的方法(除了不推荐使用的方法),或者至少可以用于任务的所有方法'试图表演;你会更好地了解班级的能力,不太可能使用不恰当/低效/不优雅的方法。

相关问题