让 NSOutlineView 滚动到新条目
首先,我对篇幅表示歉意,但我想确保我获得足够的信息。我有一个有趣的问题,实际上应该不难。我知道,如果您想让 NSOutlineView 滚动到并显示特定行,您只需使用此模式:
NSInteger row = [self.myOutlineView rowForItem:myItem];
[self.myOutlineView scrollRowToVisible:row];
我没有使用 NSTreeController 而是支持使用委托。
我正在使用适配器模式类来充当我的数据。适配器包装了许多不同的类型。它是一个简单的结构:
[适配器子级] ----- *[适配器父级]
因此适配器负责处理子级计数,可以添加,可以删除,可以扩展等,并且也充当数据传输对象。
那么,一切都解决了,这就是问题的症结所在。当我向 NSOutlineView 添加新项目时,我首先向大纲视图中选择的适配器添加一个新的子适配器。然后,我使用以下方法重新加载该项目及其子项:
[self.myOutlineView reloadItem:selectedAdapter reloadChildren:YES];
然后,我显示该对象的视图并尝试同步 NSOutlineView,以便所选行与新显示的视图相关。因此,简单来说,步骤如下:
- 最初利用 NSOutlineDataSource 协议加载 NSOutlineView 数据
- 用户选择可以添加子项目的项目(适配器)
- 用户单击“添加”按钮
- 创建新的空表示对象
- 新表示的对象被包装在适配器中
- 新创建的适配器将添加到所选适配器的子级中
- 使用新表示的对象创建新视图并显示
- 所选(父)节点及其子级将重新加载到 NSOutlineView 中
- NSOutlineView 中新创建的行是滚动到同步视图和 NSOutlineView 选择的视图
所以这就是问题所在。如果使用 rowForItem 方法,NSOutlineView 将找不到新添加的项目。然而,重新加载后,我可以展开父节点并选择新添加的项目,一切正常。为了解决这个问题,我以编程方式扩展了父节点(暴露新项目),然后尝试滚动到新项目,但它仍然找不到该行。 rowForItem 返回 -1。
在添加新项目时我是否错过了一些我没有在 NSOutlineView 中更新的内容?我尝试了很多不同的事情但没有成功,任何帮助将不胜感激。
提前致谢, 抢
First I apologize for the length but I want to ensure I get enough information out. I have an interesting problem that really shouldn't be difficult. I know that if you want to have the NSOutlineView scroll to and want display a specific row you simply use this pattern:
NSInteger row = [self.myOutlineView rowForItem:myItem];
[self.myOutlineView scrollRowToVisible:row];
I am not using a NSTreeController in favor of using delegation.
I am using an adapter pattern class to act as my data. The adapter wraps a number of different types. It is a simple structure:
[adapter children] ----- *[adapter parent]
So the adapter takes care of dealing with child count, can add, can remove, can expand, etc. and also acts like a data transport object.
So with that all out of the way here is the crux of the problem. When I add a new item to the NSOutlineView I start by adding a new child adapter to the adapter that is selected in the outline view. Then I reload the item with the children using:
[self.myOutlineView reloadItem:selectedAdapter reloadChildren:YES];
I then display the view for this object and attempt to synchronize the NSOutlineView so the selected row relates to the newly displayed view. So in a simple way the steps are:
- Initially load the NSOutlineView with data utilizing the NSOutlineDataSource protocol
- User selects an item (adapter) that can add child items
- User clicks "Add" button
- A new empty represented object is created
- The new represented object is wrapped in an adapter
- The newly created adapter is added to the children of the selected adapter
- A new view is created using the new represented object and displayed
- The selected (parent) node and its children is reloaded in the NSOutlineView
- The newly created row in the NSOutlineView is scrolled to view which synchronizes the view and the selection of the NSOutlineView
So here is the problem. The NSOutlineView will not find the newly added item if you use the rowForItem method. Yet after I reload I can expand the parent node and select the newly added item and everything works fine. In an attempt to fix the problem I have programmatically expanded the parent node (exposing the new item) then attempt to scroll to the new item and it still cannot find the row. The rowForItem returns -1.
Have I missed something when adding a new item that I am not updating something in the NSOutlineView? I've tried many different things without success and any help will be greatly appreciated.
Thanks in advance,
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了我自己的问题......终于。
由于我将第一个子节点添加到该节点,并且它从不可扩展更改为可扩展,因此需要首先扩展它,然后滚动到视图,然后选择该项目。没有更多问题了。
Solved my own problem...finally.
Since I was adding my first child to that node and it was changing from non-expandable to expandable it needed to be expanded first, then scroll to view, then select the item. No more problem.