我一直试图将NSTableView中的一个单元更改为下拉菜单,但没有成功。我阅读了Apple文档,但它没有给出如何在NSPopupButtonCell中使用NSTableView的示例。我搜索论坛,包括这里,只找到一个有点相关的例子,除了它在目标-c,所以它不工作,我的快速应用程序。表的代码如下:
extension DeviceListViewController:NSTableViewDataSource, NSTableViewDelegate{
// get the number of rows for the table
func numberOfRows(in tableView: NSTableView) -> Int {
return homedevices.count
}
// use the data in the homedevices array to populate the table cells
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{
let result = tableView.make(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
if tableColumn?.identifier == "ID" {
result.textField?.stringValue = homedevices[row].id
} else if tableColumn?.identifier == "Name" {
result.textField?.stringValue = homedevices[row].name
result.imageView?.image = homedevices[row].image
} else if tableColumn?.identifier == "Type" {
result.textField?.stringValue = homedevices[row].type
} else if tableColumn?.identifier == "Button" {
result.textField?.integerValue = homedevices[row].button
}
return result
}
// facilitates data sorting for the table columns
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
let dataArrayMutable = NSMutableArray(array: homedevices)
dataArrayMutable.sort(using: tableView.sortDescriptors)
homedevices = dataArrayMutable as! [HomeDevice]
tableView.reloadData()
}
}我真的希望能够允许下拉选择来更改分配给特定家庭设备(一个简单整数)的按钮,而不必在textfield中键入一个数字来编辑这个值。无意中,当我在IB中的表中添加弹出按钮单元格时,表单元格的所有视图都将被删除。因此,我可能需要以不同的方式创建该表。但是,我读过并尝试过的大多数内容都导致了运行时错误或显示了一个空表。
编辑:第三天:今天我一直在这里阅读:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TableView/PopulatingViewTablesWithBindings/PopulatingView-TablesWithBindings.html和其他许多地方也,但我没有代表张贴更多的链接。
我在IB中添加了一个NSPopupButton,但不知道如何设置值。我试过result.objectValue = homedevices[row].button,但这不起作用。我想我需要一个数组控制器对象。然后,我尝试为DeviceListViewController中的对象创建一个出口,比如@IBOutlet var buttonArrayController: NSArrayController!,我想我现在需要找到一种方法来将数组控制器连接到我的家用设备数组。
所以我看了这里的示例代码:https://github.com/blishen/TableViewPopup
这是在客观C,这不是我正在使用的语言,但也许如果我在一周的不同时间继续观察它,我可能会想出如何做一个下拉菜单。
因此,我继续在这方面工作,目前没有解决办法。
发布于 2017-08-12 09:09:56
这个问题已经解决了,多亏了@vadian。
该按钮被插入为NSPopUpButton对象,而不是NSPopUpButtonCell。然后单元格获得自己的自定义类,我将其称为ButtonCellView作为NSTableCellView的子类。
然后,创建的子类可以从NSPopUpButton接收一个出口到自定义子类。我可以给它一个selectedItem变量,并在这里创建菜单。然后,在表视图委托中,在创建表时,我只需将ButtonCellView对象的ButtonCellView设置为数据数组中的值。效果很好!
https://stackoverflow.com/questions/45639986
复制相似问题