3

I have an NSTableView which gets its data from an NSArrayController and I need to delete the currently selected row. I know NSManagedObjectContext has a deleteObject: method but, I can't think of how to delete it from the NSArrayController.

phaxian
  • 1,058
  • 16
  • 28
nanochrome
  • 707
  • 2
  • 12
  • 26

4 Answers4

3

Use the NSArrayController's remove: action.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
1

To remove programmatically

[NSArrayController removeObjectAtArrangedObjectIndex:NSInteger];

or

[NSArrayController removeObjectAtArrangedObjectIndexes:NSIndexset];
NewStack
  • 990
  • 8
  • 19
0

@NewStack's answer in Swift 3:

ArrayController.remove(atArrangedObjectIndex: Int)
Jerry U
  • 618
  • 9
  • 22
0

The answers from dreamlax, NewStack, styl3r are correct. But if you want to do this in code, here's the answer:

@IBOutlet var notesResultsController: NSArrayController!
@IBAction func deleteSelectedNote(_ sender: Any) {

    let selectedRow = tableView.selectedRow
    notesResultsController.remove(atArrangedObjectIndex: selectedRow)
}

Drag the NSArrayController onto your ViewController and hook up its outlet. Add a button to your interface and hook up its action to 'deleteSelectedNote'.