0

I've seen the usage like this in a subclass of an NSOutlineView

override func mouseDown(with event: NSEvent) {
    super.mouseDown(with: event)

    let location = self.convert(
        event.locationInWindow,
        to: nil)
    let clickedRow = self.row(at: location)
}

The documentation of convert(_:to:) says it

Converts a point from the view’s coordinate system to that of a given view.

So what I understood is it returns a point in the view (as the argument)'s coordinate system. In the above example, it's the window's.

The doc of row(at:) of NSTableView sasys it receives:

A point in the coordinate system of the table view.

I'm confused by the English here.

A related question is here: NSTableView: detecting a mouse click together with the row and column

Edit

Thanks to @Willeke's pointing out there's the sister of convert(_:to:), convert(_:from:). It's a coincidence that convert(_:to:) also works in my case. But I should have used convert(_:from:). I overlooked the linked example.

LShi
  • 1,500
  • 16
  • 29
  • 1
    Does this code work? The Objective-C code in the linked question uses `convert(_:from:)`. – Willeke Oct 18 '17 at 05:55
  • Yes, the `clickedRow` is the row clicked. – LShi Oct 18 '17 at 06:00
  • printed the **converted** and **original** points: There's a toolbar above the `outlineView`. The difference between the results are smaller than what I expected. “from”: Clicked (129.6484375, 41.46484375) (128.6484375, 371.53515625) “to”: Clicked (127.734375, 43.9453125) (128.734375, 369.0546875) I should have used the "from" one. But it's surprising that the `to` one also works coincidentally. – LShi Oct 18 '17 at 06:12

0 Answers0