TalkingCode
TalkingCode

Reputation: 13557

Cocoa : Remove Item from NSArrayController

I have a NSArrayController bound to a NSTableView. With this I have the typical Add/Remove buttons.

While Adding an item is very straight forward (I call a method, create an object with default values and add it) I have problems deleting objects from the controller. I know I can do this the easy way when connecting the button with the remove action from the ArrayController. But this is not what I want. I need to remove the object manually because I have some additional code to process.

Anway, removing objects seems far more complcated then I expected. It already tried:

NSArray *items =  [doToItemsArrayController selectedObjects];
[doToItemsArrayController removeSelectedObjects:items]; 

or

NSIndexSet *iSet = [doToItemsArrayController selectionIndexes];
[doToItemsArrayController removeSelectionIndexes:iSet];

None of them seems to work. How do I remove the selected Object from an NSArrayController with Objective-C code?

Upvotes: 3

Views: 3792

Answers (3)

David
David

Reputation: 14404

You can remove objects using any of these methods. They are independent from the selection.

– removeObjectAtArrangedObjectIndex:
– removeObjectsAtArrangedObjectIndexes:
– remove:
– removeObject:
– removeObjects:

If you want to remove the selected object from the content array, then you can get the selected object(s) using the below methods and then plug it into one of the methods mentioned above.

– selectionIndex
– selectionIndexes

Upvotes: 7

Georg Fritzsche
Georg Fritzsche

Reputation: 99044

You can use -removeObjects: to actually remove the objects instead of removing them from the selection:

[doToItemsArrayController removeObjects:items];

Upvotes: 1

JWWalker
JWWalker

Reputation: 22717

The methods you tried are for removing objects from the selection, not for removing objects from the array. Why not just call the remove: action method?

Upvotes: 0

Related Questions