R – Copy from one NSTableView to another (With Core Data Bindings)

copycore-dataiphonenstableview

I have a NSTableView binded to core data. I have another NSTableView, currently not binded to anything ( but that can change if need be).

When a press a button, I want the currently selected Row in the first table to be copied (but not removed) to the second table.

e.g.

Before:

First Table

Column1 | Column2 | Column3
Person  | Place   | Time

Second Table

Column1 | Column2 | Column3
        |         | 

After:

 Column1 | Column2 | Column3
 Person  | Place   | Time

Second Table

 Column1 | Column2 | Column3
 Person  | Place   | Time

How would I go about doing this?

Thanks!

Best Answer

This isn't really an issue with NSTableView, this is more a matter of program design. What do your NSTableViews display? Data. In order to take the selected object in one table view and make it appear in another, you need to figure out what data object is selected, and add it to the data set for the other table view so that it appears there, too.

It sounds like you're using bindings. If so, then you should bind your second table view to an array controller that tracks the objects that should be displayed there. Whether you need to bind that array controller to a content set depends on your application.

So, to get the selected item, check out the selectedObjects method on NSArrayController. This gives an array of the currently selected objects. You'll then need to add the selected objects to the array controller for the second table view. Once you do that, the bindings will work their magic and update your table view automatically.

Related Topic