0

I have a simple test program that has a 2 column NSTableView (View based), both columns have their cells set to NSTokenFieldCell. I also have a NSTokenField in this controller

I want to be able to drag and drop tokens from the table to the field. When I have 1 column this is working well. Now I have 2 columns I need to know which token to add to the field

Sample code:


class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSTokenFieldDelegate {

    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var tokenField: NSTokenField!

let tokens = ["tok1","tok2","tok3","tok1","tok2","tok3","tok1","tok2","tok3","tok1","tok2","tok3","tok1","tok2","tok3","tok1","tok2","tok3"]
    let tokens2 = ["image","date","time","folder"]
    override func viewDidLoad() {
        super.viewDidLoad()

   }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

 func numberOfRows(in tableView: NSTableView) -> Int {
        return max(tokens.count,tokens2.count)
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        guard let vw = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else {
            return nil
        }
        if tableColumn?.title == "Tokens" {
            vw.textField?.stringValue = tokens[row]
        }
        else
        {
            if row < tokens2.count {
                vw.textField?.stringValue = tokens2[row]
            }
            else {
            vw.textField?.stringValue = ""
            }
        }
        return vw
    }

// The bit where I need help 

func tableView(
        _ tableView: NSTableView,
        pasteboardWriterForRow row: Int)
        -> NSPasteboardWriting?
    {
            return tokens[row] as NSString //Here I need to return tokens2[row] if a token from column 2 was dragged and dropped
          
        }
    }

What I can't figure out is which column gets the click to initiate the drag and drop tableView.clickedColumn is always -1 None of the other functions such as this fire func tableView(_ tableView: NSTableView, updateDraggingItemsForDrag draggingInfo: NSDraggingInfo)

Update: I can get this function to fire as soon as I start the drag operation: func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forRowIndexes rowIndexes: IndexSet) Screen shot

I've tried to capture mouseDown but am not having any luck with it either. Any pointers?

Gazing South
  • 115
  • 8
  • "I want to be able to drag and drop tokens form the table to the field." tokens form? Or tokens from? What is the guy named 'the field'? NSTokenField? Or a guy named textField in the TableCellView? – El Tomato Jun 25 '21 at 07:09
  • Is the table view cell based or view based? Do you want to drag a table row, a table cell or a token? – Willeke Jun 25 '21 at 07:20
  • @ElTomato I want to drag and drop tokens FROM the table to the field – Gazing South Jun 25 '21 at 07:26
  • @Willeke - view based table (Updated above - thanks) I want to be able to drag a token - (When it's one column this works as I don't have to figure out which column the token comes from – Gazing South Jun 25 '21 at 07:28
  • I don't know a guy named 'the field.' Show a picture, which may help some people but me. – El Tomato Jun 25 '21 at 07:29
  • @ElTomato - updated with screen shot – Gazing South Jun 25 '21 at 07:42
  • So both columns have a `NSTokenField` as their text field? Do you want to drag a token from one `NSTokenField` to another? Are the token fields in the cells selectable or editable? – Willeke Jun 25 '21 at 07:43
  • Are the tokens in the rows related or are they two lists? – Willeke Jun 25 '21 at 07:45
  • @Willeke yes both columns have ```NSTokenFieldCell``` as their text field I want to drag and drop a token from either column into the field below the table (It's a ```NSTokenField```) The token fields are selectable but not editable If I have one column then this is easy ```return tokens[row] as NSString``` When there is only one column the token copied to the pasteboard is alway the right one, but when there are 2 columns I need to know which columns token I need to copy – Gazing South Jun 25 '21 at 07:50
  • Why can't you drag a token? `pasteboardWriterForRow` is for dragging rows. – Willeke Jun 25 '21 at 07:53
  • @Willeke how would I go about dragging a token? – Gazing South Jun 25 '21 at 07:54
  • `NSTokenField` supports drag & drop of tokens. – Willeke Jun 25 '21 at 08:08
  • @Willeke I understand that nstokenfields support drag and drop but I still need to get the token out of the tableview - I have other apps that have drag and drop of token between 2 token fields. This is the first time between a nstokenfieldcell embedded in a nstableview and a nstokenfield – Gazing South Jun 25 '21 at 08:55
  • Fixed it - set NSTokenFieldCell to selectable (it had been set to none for some reason, don't recall doing it). Now the right token gets pasted. – Gazing South Jun 25 '21 at 09:41

1 Answers1

0

The NSTokenFieldCell didn't have its property set to selectable once set the NTTokenFieldCell and the NSTokenField worked as desired.

Gazing South
  • 115
  • 8