Replacing model objects using an NSArrayController
Replacing model objects using an NSArrayController
- Subject: Replacing model objects using an NSArrayController
- From: William Peters <email@hidden>
- Date: Thu, 11 Feb 2010 21:33:02 -0700
Hello everyone,
I'm a bit of a newbie here, but hopefully this question won't be too annoying. :-)
My application contains several records displayed to the user in an NSTableView bound to an NSArrayController. Because these records are complicated (containing around thirty fields), I have implemented an Edit option where the user can select one of rows in the table, click the Edit button, and have a new window appear displaying all of the fields, radio buttons, numeric values, etc. for changing.
I have implemented NSCopying on my data objects. When I go to display the edit window, I get a reference to the selected object (myController arrangedObjects / selectionIndexes / objectAtIndex), copy the data into a new object for editing ([myObject copy]) and then pass that off to the NSWindowController that manages the editing process. I do this so that a Cancel from the editor window will not affect the original object.
All of that seems to be straightforward enough.
My question is how to get the new, edited object back into the array controller.
I have implemented all of the KVC methods in the Manager class that supplies the data to the NSArrayController, and they are being called for Add and Delete. I've also coded a replaceObjectInMyArrayAtIndex:withObject: which I would like to be called by the array controller when it swaps in the new object for the old one. Although my current code does the same thing with separate remove and insert calls (see below), that seems to lead to two undoable actions, rather than the one I have coded in replaceObjectInMyArrayAtIndex:withObject:
fSrcbook = [selArray objectAtIndex: selItem];
fEditbook = [fSrcbook copy];
// display editing window, etc.
if (returnCode == NSOKButton) {
NSArray * bka = [fArrayController arrangedObjects];
int row = [bka indexOfObjectIdenticalTo: fSrcItem];
[fArrayController removeObject: fSrcItem];
[fArrayController insertObject: fEditItem atArrangedObjectIndex: row];
}
(I suppose I could put the remove/insert in an undo edit group, but I'd like to keep my undo code out of this method, if possible. No particular reason, except that it feels cleaner to me.)
I can't see anything in the NSArrayController class that will help me do the replace directly, and nothing else relevant has popped up in my searching for a solution. I admit, however, that I am sometimes blind to the obvious, and I also may have used inadequate search terms. Still, I highly doubt I'm the first person to want to do this, and it just seems like it should be possible to do a replace directly.
And I am also wondering if this is the usual way to implement editing a complicated object like I am attempting to do.
Any suggestions or clarifications would be greatly appreciated.
Thank you,
Bill
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden