How to use combo box cell in a tableView

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:

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?


Answered by Claude31 in 666573022
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 :
Code Block
    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        return states[row]
    }

Code Block
I changed func numberOfItemsInComboBoxCell(in comboBoxCell: NSComboBoxCell) -> Int {

to
Code Block
func numberOfItems(in comboBoxCell: NSComboBoxCell) -> Int {

and the error went away, however it still does not work. Also, I put a breakpoint on the function " func comboBoxCell" and it does not get called.

Any Ideas how to resolve?
Why don't you define also, line 25:

Code Block
NSComboBoxCell.delegate = self


AFAIK, that's needed to detect selection in Combo.
I get an error when I added the following statement
Code Block
NSComboBoxCell.delegate = self

The error was no delegate member for NSComboBoxCell..

So, I created a new test project which had a single view and a tableView that I added a comboBox to one of the columns without trying to use a datasource, I expected to see Item 1, Item 2 .. etc. in the column, but again nothing. Also, the comboBox is editable.

There must be something basic I am missing. Still looking for an example..
Sorry, I typed it too quickly.
You have to add :
Code Block
        itemComboCell.delegate = self

and NOT
Code Block
NSComboBoxCell.delegate = self

Accepted Answer
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 :
Code Block
    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        return states[row]
    }

Thanks to Claude for getting this working..
How to use combo box cell in a tableView
 
 
Q