简体   繁体   中英

NSTableView: Directly editing a textfield without highlighting the whole row

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)
    }
}

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