swift - 使用 swift 用 NSPopupButtonCell 填充 NSTableView 行

标签 swift macos nstableview

我一直在尝试将 NSTableView 中的一个单元格更改为下拉菜单,但没有成功。我阅读了 Apple 开发人员文档,但它没有给出如何在 NSTableView 中使用 NSPopupButtonCell 的示例。我搜索了论坛,包括这里,只找到了一个有点相关的例子,除了它是在 Objective-C 中,所以它不适用于我的 swift 应用程序。该表的代码在这里:

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()
}


}

我真的只是希望能够允许下拉选择来更改分配给特定家庭设备的按钮(一个简单的整数),而不必在文本字段中输入数字来编辑该值。不幸的是,当我将 popupbuttoncell 添加到 IB 中的表格时,表格单元格的所有 View 都被删除。所以我可能需要以不同的方式创建表。但我读过并尝试过的大多数内容都会导致运行时错误或显示空表。

编辑: 第三天: 今天我一直在这里阅读:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TableView/PopulatingViewTablesWithBindings/PopulatingView-TablesWithBindings.html 以及许多其他地方,但我没有代表发布更多链接。

我在IB中添加了一个NSPopupButton,但不知道如何设置该值。我尝试了 result.objectValue = homedevices[row].button,但这不起作用。我想我需要一个数组 Controller 对象。然后我尝试在我的 DeviceListViewController 中为该对象创建一个导出,例如 @IBOutlet var buttonArrayController: NSArrayController! 我想我现在需要以某种方式找到一种将阵列 Controller 连接到我的 homedevices 阵列的方法。

所以我在这里查看了示例代码: https://github.com/blishen/TableViewPopup

这是用 Objective-C 编写的,这不是我使用的语言,但如果我在一周的不同时间继续查看它,也许我可能会弄清楚如何制作一个下拉菜单。

所以我正在继续解决这个问题,目前没有解决方案。

最佳答案

这个问题已经解决,感谢@vadian。
该按钮作为 NSPopUpButton 对象插入,而不是 NSPopUpButtonCell。 然后单元格获得自己的自定义类,我将其称为 ButtonCellView 作为 NSTableCellView 的子类。
然后创建的子类可以接收从 NSPopUpButton 到自定义子类的导出。我可以给它一个 selectedItem 变量并在此处创建菜单。 然后在 TableView 委托(delegate)中,在制作表格时,我可以将 ButtonCellView 对象的 selectedItem 设置为数据数组中的值。 效果很好!

关于swift - 使用 swift 用 NSPopupButtonCell 填充 NSTableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639986/

相关文章:

swift - NSManagedObject更改不会触发objectWillChange

SKPhysicsBody 的 Swift 便捷初始化器扩展

ios - 如何从 API 访问文本响应。 swift ?

swift - 将 CAShapeLayer 转换为在 NSImageView 中使用

cocoa - 当项目更改时,CoreData 绑定(bind)的 NSTableView 会失去输入焦点,但前提是已排序

ruby-on-rails - RVM:无法安装 ruby

java - 如何在 macOS 上使用 JavaFX 打开应用程序的新实例

linux - 如何以跨平台方式将彩色打印到控制台?

swift - 从后台线程合并到主线程上下文 : delete order random so crashes NSTableView

objective-c - NSTableView 内的 NSButton 以编程方式?