如何从NSTableView的NSTableCell中获取值?

问题描述 投票:-1回答:1

问题很简单。如何编写快速代码以在选择行中获取单元格字符串值(当我单击NSTableView中的行时)?如果更好,我想在数组中获取selectrow值。

tableView looks

我已经可以打印出selectrowIndex。如果我的标识符不正确,请帮助我更正它。如果有人可以指导我,那将很有帮助。

代码:

扩展名ViewController:NSTableViewDataSource,NSTableViewDelegate {

func numberOfRows(in tableView: NSTableView) -> Int {
    return (data.count)
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    let person = data[row]

    guard let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else { return nil}
    cell.textField?.stringValue = person[tableColumn!.identifier.rawValue]!
    //cell.textField?.stringValue = "XXX";
    return cell
}

func tableViewSelectionDidChange(_ notification: Notification) {

    let selectedRow = self.tableView.selectedRow
    if (selectedRow > -1) {

        // What should I do here???
        print(selectedRow)

    }
}

}

Identifier setup 1

Identifier bind

swift nstableview nstablecellview nstablecolumn
1个回答
0
投票

感谢vadian,我已经可以从模型中检索数组值。数据就是我放在tableView中的内容。

代码:

覆盖func viewDidLoad(){super.viewDidLoad()

    // Do any additional setup after loading the view.
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    // Do any additional setup after loading the view.
    let mySerialPortNameArray = getSerialPortList();
    parsePortList(deviceList:mySerialPortNameArray,dataForView:&data);
    tableView.reloadData();
}

代码:

扩展名ViewController:NSTableViewDataSource,NSTableViewDelegate {

func numberOfRows(in tableView: NSTableView) -> Int {
    return (data.count)
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    let person = data[row]

    guard let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else { return nil}
    cell.textField?.stringValue = person[tableColumn!.identifier.rawValue]!
    //cell.textField?.stringValue = "XXX";
    return cell
}

func tableViewSelectionDidChange(_ notification: Notification) {

    let selectedRow = self.tableView.selectedRow
    if (selectedRow > -1) {

        print(data[selectedRow])
        print(selectedRow)

    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.