I have a test app with a tableView that has a comboBoxCell for one of the columns in Xcode 12:
The view controller code is as follows:
The view controller code is as follows:
Code Block // // ViewController.swift // ComboBoxExample // // Copyright © 2016 yourCompany. All rights reserved. // import Cocoa class ViewController: NSViewController, NSComboBoxCellDataSource { var states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"] @IBOutlet weak var itemComboCell: NSComboBoxCell! override func viewDidLoad() { super.viewDidLoad() itemComboCell.usesDataSource = true itemComboCell.dataSource = self self.itemComboCell.completes = true // Do any additional setup after loading the view. } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } func numberOfItemsInComboBoxCell(in comboBoxCell: NSComboBoxCell) -> Int { // anArray is an Array variable containing the objects return states.count } func comboBoxCell(_ comboBoxCell: NSComboBoxCell, objectValueForItemAt index: Int) -> Any { return (states[index]) } } When running the example I get the following error: 2021-03-05 15:16:29.940373-0600 ComboBoxExample[40515:1914724] *** Illegal NSComboBoxCell data source (<ComboBoxExample.ViewController: 0x600000e501b0>). Must implement numberOfItemsInComboBoxCell: and comboBoxCell:objectValueForItemAtIndex: How do I resolve this?
Here is a solution, if the cell content for column is defined as ComboBox in IB.
In IB, declare that comboBox uses dataSource (in Attributes inspector).
Always in IB, set the ViewController as the comboBox data Source
If you want the cell to show some initial content, add the func :
In IB, declare that comboBox uses dataSource (in Attributes inspector).
Always in IB, set the ViewController as the comboBox data Source
If you want the cell to show some initial content, add the func :
Code Block func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? { return states[row] }