我有2个组合框,它们使用与数据源相同的字典。在第一个组合框中将显示键,在第二个组合框中将显示值。
首先,我需要一些帮助来实现数据源委托方法;请参阅下面的代码清单。相关的方框是floorsBox和roomsBox。忽略其他2,因为它们将从本地变量或界面填充。 另外,假设对于第一层[key = 0]我有3个房间[value = 3],我如何取3的值并将其变成roomBox内的3个项目;因此,每次楼层更改时,房间的方框都有相应数量的项目(如果有意义的话)。我真的很感谢你的帮助,我感谢你。
class RoomAndAlarmTypesVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
//MARK: - Properties
private var floorsAndRooms = [String: String]()
@IBOutlet private weak var floorsBox: NSComboBox!
@IBOutlet private weak var roomsBox: NSComboBox!
@IBOutlet private weak var roomType: NSComboBox!
@IBOutlet private weak var alarmType: NSComboBox!
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
floorsAndRooms = representedObject as! [String : String]
floorsBox.usesDataSource = true
roomsBox.usesDataSource = true
roomType.usesDataSource = true
alarmType.usesDataSource = true
floorsBox.delegate = self
roomsBox.delegate = self
roomType.delegate = self
alarmType.delegate = self
floorsBox.dataSource = self
roomsBox.dataSource = self
roomType.dataSource = self
alarmType.dataSource = self
}
@IBAction private func setTheSystem(_ sender: NSButton) {
}
func numberOfItems(in comboBox: NSComboBox) -> Int {
what do i do here?
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
same here?
}
}
答案 0 :(得分:2)
使用基于发件人的switch语句(您的comboBox参数)
例如:
switch comboBox
{
case floorsBox : return // the data for floors
case roomsBox : return // the data for rooms
...
}