仅在选定行上执行NSTableView

时间:2018-01-24 14:02:56

标签: swift cocoa nstableview nscombobox

我正在使用可编辑的行列构建基于视图的NSTableView。用户完成编辑行然后使用鼠标/键盘选择另一行后,我想提示用户询问是否应该保存刚更改的行。我能够通过处理NSTableViewDelegate shouldSelectRow通知来实现这一点 - 我可以查看当前选定的行,如果更改则提示用户。

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

    return true
}

当我向表中添加NSComboBox列时,我的问题就出现了。现在,用户可以通过单击箭头按钮并从下拉列表中选择值,在另一个行中使用鼠标在组合框中选择值。这不会更改表的选定行。所以我没有得到shouldSelectRow通知。

如果以某种方式我也可以选择同时点击组合框向下箭头的行,我的问题将得到解决。这应该通知我,然后它应该工作。我查看了实现NSComboBoxDelegate,以便在组合框中更改值选择时获得通知,但在该通知中,我得不到有关组合框哪一行的信息。

如何将选择更改为单击组合框箭头按钮的行?

这是我的tableView方法

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
    }

理想情况下,除非用户首先选择该行,否则我希望阻止其他行中的组合框被点击/编辑。这就是我现在要完成的目标

0 个答案:

没有答案
相关问题