开发者

Deleting an object in NSArrayController

开发者 https://www.devze.com 2022-12-16 23:17 出处:网络
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

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'.

0

精彩评论

暂无评论...
验证码 换一张
取 消