我有一个测试应用程序与tableView有一个comboBoxCell在Xcode 12的列之一:
The view controller code is as follows:
//
// ViewController.swift
// ComboBoxExample
//
//
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"]
// var itemCombo: NSComboBoxCellDataSource? = states
@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])
}
}
运行示例时,我得到以下错误:[40515:1914724] ***非法的NSComboBoxCell数据源(<ComboBoxExample)。ViewController:>)。必须实现numberOfItemsInComboBoxCell:和comboBoxCell:objectValueForItemAtIndex:
我做错了什么试图设置数据源如下:itemComboCell。dataSource = self
感谢下面是这个测试应用程序的故事板图片。
输入图片描述
Objective-C
- (NSInteger)numberOfItemsInComboBoxCell:(NSComboBoxCell *)comboBoxCell;
在Swift
中optional func numberOfItems(in comboBoxCell: NSComboBoxCell) -> Int
来源:numberOfItemsInComboBoxCell