0

I'm building a view-based NSTableView with editable row columns. After user finishes editing a row and then selects another row using mouse/keyboard I want to prompt user asking if row just changed should be saved. I was able to get this working by processing NSTableViewDelegate shouldSelectRow notification - I can look at currently selected row and if changed prompt the user.

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
    // if tableView.selectedRow has changed prompt user to save first

    return true
}

My problems started when I added a NSComboBox column to the table. Now user can simply select value in the combo box using the mouse in another row by clicking the arrow button and selecting value from drop down. This does not change the selected row of the table. So I don't get shouldSelectRow notification.

My problem will be solved if somehow I can also select the row in which the combo box down arrow is being clicked at the same time. This should deliver a notification to me and then it should work. I looked at implementing NSComboBoxDelegate to get informed when value selection was changing in a combo box, but in that notification I get no information on which row the combo box is.

How can I also change the selection to the row where the combo box arrow button is being clicked?

Here's my tableView method

extension ViewController: NSTableViewDelegate {
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cellView: NSTableCellView
        cellView  = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView

        if let transaction = account?.transactions[row] {
            switch tableColumn?.identifier.rawValue {
            case Column.date.rawValue?:
                cellView.textField?.stringValue = Date.fromDate(transaction.date)
            case Column.description.rawValue?:
                cellView.textField?.stringValue = transaction.desc
            case Column.type.rawValue?:
                var typeCombo = cellView.subviews[0] as! NSComboBox
                switch transaction.type {
                case .spend:
                    typeCombo.selectItem(withObjectValue: TransactionType.spend.rawValue)
                case .receive:
                    typeCombo.selectItem(withObjectValue: TransactionType.receive.rawValue)
                }
            case Column.amount.rawValue?:
                cellView.textField?.doubleValue = transaction.amount
            default:
                print("Default... - \(tableColumn?.identifier)")
                break
            }
        }

        return cellView
    }

Ideally what i would like it to prevent the combo box in other row from being clicked/editable unless user selects that row first. This is what I'm trying to accomplish now

griftopia
  • 135
  • 1
  • 3
  • 10
  • Possible duplicate of [Get button's row in view based table](https://stackoverflow.com/questions/10202813/get-buttons-row-in-view-based-table) – Willeke Jan 24 '18 at 14:13
  • Show your tableView objectValueFor tableColumn delegate method. – El Tomato Jan 24 '18 at 23:04
  • I am not using objectValueFor, I'm using tableView. Code too long to paste here, will try in separate entry. – griftopia Jan 24 '18 at 23:43

0 Answers0