I have a NSTextfield
in a TableView
. The content mode is configured to ViewBased
. So I have the type: NSTableCellView
.
Example of my Table View:
My problem is that I can't edit until I have selected the line. However, I want to click on the text field independently of the line and edit directly without selecting the whole line.
Current solution: In the Table View
I can currently set the highlight to None
and then direct editing works, but I still want to be able to select the whole row when I click outside the Textfield
in the row.
I would be very happy if someone could help me. Thanks a lot!
Disclaimer - this is adapted after the nice answer from here.
One possible approach is to subclass NSTableView
and override hitTest
in order to send the user interaction events directly to the text field instead of the cell view:
override func hitTest(_ point: NSPoint) -> NSView? {
let myPoint = superview!.convert(point, to: self)
let column = self.column(at: myPoint)
let row = self.row(at: myPoint)
// if we find an editable text field at this point, we return it instead of the cell
// this will prevent any cell reaction
if row >= 0, column >= 0,
let cell = view(atColumn: column, row: row, makeIfNecessary: false),
let textField = cell.hitTest(convert(myPoint, to: cell.superview)) as? NSTextField,
textField.isEditable {
return textField
} else {
return super.hitTest(point)
}
}