获取 NSArrayController 内容的每个元素的唯一标识符
我正在制作一个自定义视图,我希望它与绑定/核心数据兼容并表示数据集合(la NSTableView)
有没有什么方法我的视图可以引用集合中元素的特定子集(除了当前选择)在用户更改后?
一些背景信息:
该视图将在 2D 空间中显示许多用户可移动的框。每个框对应于模型中的一条记录。可以一次移动几个,并且我不能依赖每个框的增量值相同(因此不需要向每个选定的对象添加增量)。
我想我正在寻找类似由 NSArrayController 分配给内容数组的每个元素的 id 之类的东西,以便视图可以将该 id 与每个框相关联。我的第一个想法是使用内容数组中的索引,但这可能会被撤消/重做搞乱。我可以继承 NSArrayController 并让它为每个模型项自动生成一个 id,但是 cocoa 已经做了类似的事情了吗?感觉我可能会错过一些东西。
I'm making a custom view that I want to be bindings/core data compatible and represent a collection of data (a la NSTableView)
Is there any way my view can refer to a specific subset of the elements in the collection (other than the current selection) after a change by the user?
A bit of context:
The view is going to display a number of user-moveable boxes in a 2D space. Each box corresponds to a record in the model. Several can be moved at once, and I can't rely on the delta value being the same for each box (so no adding a delta to each selected object).
I guess I'm looking for something like an id assigned to each element of the content array by NSArrayController, so that the view can associate that id with each box. My first thought was to use the the index in a content array, but this could be messed up by undo/redo. I could subclass NSArrayController and get it to auto-generate an id for each model item, but does cocoa already do something like this already? Feels like I might be missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我应该提到的是,我最初尝试将每个内容数组的元素存储在视图中(正如 Peter 建议的那样),但将它们作为键存储在字典中。
视图中的对象与字典中的键不匹配,所以我认为这意味着 NSArrayController 更改了它用来代表模型对象的代理对象。
然而,事实证明 NSDictionary 会复制它的键,因此对于要将对象的特定实例与另一个对象关联的情况似乎没有什么好处。
NSMapTable 是它更灵活的表兄弟,可以配置为不复制其键。
I should have mentioned that I originally tried keeping each of the content array's elements stored in the view (as Peter suggests), but had them stored as keys in an dictionary.
The objects in the view didn't match the keys in the dictionary, so I assumed this meant that NSArrayController changed the proxy objects it uses to stand for model objects.
However, it turned out that NSDictionary copies its keys, so it seems to be no good for situations where you want to associate a particular instance of an object with another.
NSMapTable is its more flexible cousin, and can be configured not to copy its keys.
为什么不直接引用对象本身呢?您可以将它们保存在一个集合或数组中,无论哪一个合适。
如果您确实需要某种标识符:有什么用?你打算用它做什么?
Why not just refer to the objects themselves? You can keep them in a set or array, whichever is appropriate.
If you really need an identifier of some sort: What for? What are you going to do with it?