我正在尝试将NSTextView
绑定到代码中的NSArrayController
,并且已经关注了一些问题:here,here和here。我正在使用Xcode 9.2和Swift 4构建一个MacOS应用程序。
我有一个带有2个按钮的SegmentedControl
:一个用于添加新对象,另一个用于删除。当我尝试将NSTextField
绑定到添加到数组中的新创建的对象时,我崩溃了。
我的VeiwController
代码:
private enum Columns {
static let name = NSUserInterfaceItemIdentifier("NameColumn")
static let age = NSUserInterfaceItemIdentifier("AgeColumn")
}
let arrayController: NSArrayController
let table: NSTableView
let buttons: NSSegmentedControl
// MARK: —Initialisation—
required init() {
super.init(nibName: nil, bundle: nil)
/// Code to create arrayController, table, and buttons
///...
/// Bindings.
table.bind(.content,
to: self.arrayController,
withKeyPath: "arrangedObjects",
options: nil)
/// Delegates
table.delegate = self
buttons.target = self
buttons.action = #selector(splitButtonClicked)
}
从arrayController
添加/删除对象的功能。 Person
是一个简单的类,有2个属性(Name
和Age
):
@objc func splitButtonClicked() {
switch buttons.selectedSegment {
case 0:
let person = Person()
person.name = "Alfred"
person.age = 21
arrayController.addObject(person)
case 1:
arrayController.removeObject(arrayController.selectedObjects)
default:
print("Switch case error")
}
}
创建新表行的代码返回一个新的NSTextField
,但是当我尝试绑定文本字段时我崩溃了,我不知道为什么:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let column = tableColumn else { return nil }
let t = NSTextField(frame: NSRect(x: 0, y: 0, width: column.width, height: table.rowHeight))
if column.identifier == Columns.name {
/// Crashing on this line.
t.bind(.value, to: t, withKeyPath: "objectValue.name", options: nil)
} else if column.identifier == Columns.age {
t.bind(.value, to: t, withKeyPath: "objectValue.age", options: nil)
}
return t
}
这2行:
let t = NSTextField(...)
t.bind(.value, to: t, withKeyPath: "objectValue.name", options: nil)
您将文本字段绑定到不存在的密钥路径上。如果展开表视图的视图层次结构,它将如下所示:
Table View > Column > Cell View > Text Field
您需要提供的是Cell View,文本字段嵌套在其中。
fileprivate extension NSUserInterfaceItemIdentifier {
static let cellView = NSUserInterfaceItemIdentifier("CellView")
static let name = NSUserInterfaceItemIdentifier("NameColumn")
static let age = NSUserInterfaceItemIdentifier("AgeColumn")
}
extension ViewController: NSTableViewDelegate, NSTableViewDataSource {
private func makeCell(frame: CGRect) -> NSTableCellView {
// Reuse an existing cell if possible
if let existingCell = table.makeView(withIdentifier: .cellView, owner: nil) as? NSTableCellView {
return existingCell
}
// Make a new cell
let textField = NSTextField(frame: frame)
textField.drawsBackground = false
textField.isBordered = false
let cell = NSTableCellView(frame: frame)
cell.identifier = .cellView
cell.textField = textField
cell.addSubview(textField)
return cell
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let column = tableColumn else { return nil }
let cell = makeCell(frame: NSRect(x: 0, y: 0, width: column.width, height: table.rowHeight))
// Varying the binding based on what column is being requested
switch column.identifier {
case .name:
cell.textField?.bind(.value, to: cell, withKeyPath: "objectValue.name", options: nil)
case .age:
cell.textField?.bind(.value, to: cell, withKeyPath: "objectValue.age", options: nil)
default:
print("Unrecognized column '\(column.identifier)'")
}
return cell
}
}