开发者

how to copy rows from NSTableView

I have one NSTableView, Which displays just the row of numbers. I just want to copy those numbers using (cmd+copy) or righ开发者_JS百科t click copy. How can I achieve this feature in NSTableViews.


This is how I got it to work, and it behaves as expected with the responder chain (it is only called when the NSTableView itself becomes first responder, and not when editing table cells etc.).

It's a two-part solution:

Part 1

Adding copy(_:) and paste(_:) methods to the NSTableView subclass enables code to be run when the Edit→Copy or Edit→Paste menu items are triggered by the user. The responder chain recognizes these as selector methods.

Part 2

Conforming the NSTableView subclass to NSMenuItemValidation so we can validate whether those Copy or Paste menu items should be enabled for the user. (This is not automatic in this scenario, so we have to write that logic ourselves).

How To

First, in your Main Menu (storyboard), find the Edit menu and open the Attributes Inspector. Then:

  • set the Identifier for Copy to "menuEditCopy"
  • set the Identifier for Paste to "menuEditPaste"

Then, this extension can be used as a template, based on an NSTableView subclass called MyGreatTableView.

class MyGreatTableView: NSTableView {
    ...
}

extension MyGreatTableView: NSMenuItemValidation {
    
    override var acceptsFirstResponder: Bool { true }
    
    @IBAction func copy(_ sender: AnyObject?) {
        // add your logic to copy the selected rows to the clipboard
    }
    
    @IBAction func paste(_ sender: AnyObject?) {
        // add your logic to paste rows from the clipboard
    }
    
    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
        switch menuItem.identifier {
        case NSUserInterfaceItemIdentifier("menuEditCopy"):
            // enable Copy if at least one row is selected

            return numberOfSelectedRows > 0
            
        case NSUserInterfaceItemIdentifier("menuEditPaste"):
            // enable Paste if clipboard contains data that is pasteable
            
            // ... 
            // (add your logic to read the clipboard
            // and conditionally enable Paste here)
            // ...
            
        default:
            return false
            
        }
    }
    
}


By default, the edit menu (cut/copy/paste/etc) is auto-enabled, provided the text in your tableView row is selectable or editable, and the text field in question is in the responder chain.

The auto-enabled menu items work by looking at the responder chain. If the 1st responder responds to the selector that the menu item is tied to, the menu item is enabled. If the 1st responder doesn't respond to the selector of a given menu item, the menu item is disabled

You'll want to make sure the text field element in your table cell/view is selectable (you can turn this on/off in interface builder), also make sure your text field acceptsFirstResponder (might be called allowsFirstResponder, I'm going by memory)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜