views:

36

answers:

2

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.

A: 

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?

Peter Hosey
Thanks. Yes as a general answer to my question, you're right. I figured out what was wrong in my case and posted the details below.
Chris Devereux
+1  A: 

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.

Chris Devereux