How to change selectItem in NSPopUpButtonCell NSTableView Swift 4.2?

174 Views Asked by At

I wanna change index of popUp button in NSTableView.

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {

    let dataCell:NSPopUpButtonCell = tableColumn?.dataCell as! NSPopUpButtonCell
        dataCell.addItems(withTitles: dataTypes)
        return data.type //dataCell 

}

func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) {

    dataSourceArr[row].type = dataTypes[object as! Int]
        tableView.reloadData()

}

I can update my dataSource array but I can't update in tableView.

1

There are 1 best solutions below

0
Bharat Gadde On

You can use NSTableViewDelegate's willDisplayCell method to achieve this.

extension ViewController: NSTableViewDelegate {

    func tableView(_ tableView: NSTableView, willDisplayCell cell: Any, for tableColumn: NSTableColumn?, row: Int) {

        guard let dataCell = cell as? NSPopUpButtonCell else {return}
        dataCell.selectItem(at: row) // or dataCell.selectItem(withTitle: //title)
    }
}