简体   繁体   中英

How can I change the double-click action for only a single column of NSTableview

Okay, so I have two projects that have NSTableViews. One has a single column with dates (in a , and my implementation for letting users change the dates is by changing the NSTableview's doubleAction to display an NSPopover with a graphical date picker, like so:

override func viewDidLoad() {

    super.viewDidLoad()
    self.changeAction()
}

func changeAction() {

    self.entriesTableView.target = self
    self.entriesTableView.doubleAction = "displayPopUp:"
}

displayPopUp(sender: AnyObject) {

    var row = self.entriesTableView.selectedRow
    var rowView:NSTableRowView? = self.entriesTableView.rowViewAtRow(row, makeIfNecessary: true) as NSTableRowView?
    var rect = rowView?.bounds
    if (rowView != nil){

        var popover = NSPopover()
        var storyboard = NSStoryboard(name: "Main", bundle: nil)
        if (storyboard != nil) {

            var popoverVC: CalendarPopupViewController = storyboard!.instantiateControllerWithIdentifier("calendarView") as CalendarPopupViewController

            popover.contentViewController = popoverVC
            var selectedEntry:Entry = self.entriesArrayController.selectedObjects[0] as Entry

            popover.showRelativeToRect(rect!, ofView: rowView!, preferredEdge: NSMaxXEdge)
            popoverVC.entryVC = self
            popoverVC.entriesAC = self.entriesArrayController
            popoverVC.datePicker.bind("dateValue", toObject: selectedEntry, withKeyPath: "date", options: nil)
            popover.behavior = NSPopoverBehavior.Transient
            self.datePopover = popover
        }
    }
}

This works pretty well, and I like it much better for this situation than forcing users to use the textual date pickers with steppers.

That latter solution, textual date pickers with steppers, is what I have in my other app. I'd like to replace it with something like the solution above, BUT here's the catch: In the second app, the NSTableview has three columns, one for the date and two others with editable textfields. So, I'm not sure how to set it up so that double-clicking on a date pulls up the popover, but clicking on the other columns functions just like normal...I don't want the date popup showing when the user has double clicked on "title" for instance, only when they double click on the date!

Any ideas?

In your double-click action method, check the table view's clickedColumn to learn which column was double-clicked. You can look up the corresponding NSTableColumn by indexing into the table view's tableColumns array. Then, you can check the identifier of the table column. If it's not the one you want to present the pop-up for, just return.

Setting the double-click action does have the side effect of disabling double-click-to-edit. If you want to restore that functionality, you need to do that before returning. For an NSCell -based table view, you can do that by calling -editColumn:row:withEvent:select: on the table view.

For a view-based table view, it's a bit more complicated. -editColumn:row:withEvent:select: merely attempts to make the cell view the first responder. If the cell view is something simple like an NSTextField , this works. However, if it's a compound view like an NSTableCellView , it won't accept first responder. You'll need to somehow pick an appropriate subview within the cell view and make that the first responder (Objective-C code):

NSView* targetView = /* ... somehow pick a view ... */;
if ([targetView acceptsFirstResponder])
    [targetView.window makeFirstResponder:targetView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM