自动选择、聚焦并突出显示新的 NSOutlineView 行
这可能只是缺乏 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:
If I click away, then select the row and press enter
to edit it, it now looks like this:
My question is: How can I programmatically get the same state (focus, selected, highlighted) the first time, to make the user experience better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的事情对我有用:
您可以使用
rowForItem:
而不是重复检查itemAtRow:
。您通常还需要使用
[sourceList rollRowToVisible:...]
以防新行不可见,并且您可以使用noteNumberOfRowsChanged
而不是reloadData,除非数据实际上已更改。
标准的 Mac 行为是选择新创建的项目的内容,因此请使用
select:YES
。如果这没有帮助,那么您的代码中还存在其他内容,上面的代码片段无法传达...
一般来说,我真的建议在学习新课程时,您可以从头到尾阅读文档页面列表可用的方法(不推荐使用的方法除外),或者至少是可用于您尝试执行的任务的所有方法;您将更好地了解该类的功能,并且不太可能使用不适当/低效/不优雅的方法。
Something like this works for me:
You can use
rowForItem:
instead of repeatedly checkingitemAtRow:
.You generally also want to use
[sourceList scrollRowToVisible:...]
in case the new row isn't visible and you can usenoteNumberOfRowsChanged
instead ofreloadData
, 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.