Deleting an object in NSArrayController
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 fro开发者_JS百科m the NSArrayController
.
Use the NSArrayController
's remove:
action.
To remove programmatically
[NSArrayController removeObjectAtArrangedObjectIndex:NSInteger];
or
[NSArrayController removeObjectAtArrangedObjectIndexes:NSIndexset];
@NewStack's answer in Swift 3:
ArrayController.remove(atArrangedObjectIndex: Int)
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'.
精彩评论