我正在通过 Swift书中的Cocoa工作,我被困在关于Bindings的章节中.本书使用nib文件,但我想以编程方式完成所有操作(因为我加入了一个不使用nib的团队).该项目是创建一个基于视图的表,其中
我能够以编程方式创建表,如下所示(一个滚动视图,一个表视图,两个表列):
let tableWidth = windowWidth! * 0.6
let tableHeight = windowHeight! * 0.8
scrollView = NSScrollView(frame: NSRect(x: windowWidth!*0.05, y: windowHeight!*0.08, width: tableWidth, height: tableHeight))
employeeTable = NSTableView(frame: NSRect(x: 0, y: 0, width: tableWidth, height: tableHeight))
employeeTable?.bind("content", toObject: (self.arrayController)!, withKeyPath: "arrangedObjects", options: nil)
nameColumn = NSTableColumn(identifier: "name column")
nameColumn?.width = tableWidth * 0.4
nameColumn?.headerCell.title = "Name"
raiseColumn = NSTableColumn(identifier: "raise column")
raiseColumn?.width = tableWidth * 0.6
raiseColumn?.headerCell.title = "Raise"
employeeTable?.addTableColumn(nameColumn!)
employeeTable?.addTableColumn(raiseColumn!)
employeeTable?.setDelegate(self)
scrollView?.documentView = employeeTable
如您所见,我不知道该表是基于单元格还是基于视图.我怎么知道我的桌子是基于什么的?由于本章是关于Bindings的,因此没有使用任何委托或数据源方法,我也想这样做.
下一个问题:
就像我说的那样,本书使用了NIB,可以随时访问NSTableView,NSTableCellView和它下面的NSTextField.首先,NSTableView的内容绑定到数组控制器的arrangeObjects.我能够以编程方式执行此部分,因为我自己在代码中创建了tableView对象.然后,本书将NSTextField的值绑定到NSTableCellView的objectValue.name(name是Employee对象的属性之一).我怎么做这部分,因为我没有在我的代码中创建这些NSTableCellView和NSTextField对象?有没有办法访问它们(假设我的表甚至基于视图)?
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let frameRect = NSRect(x: 0, y: 0, width: tableColumn!.width, height: 20)
let tableCellView = MyTableCellView(frame: frameRect)
if tableColumn?.identifier == "name column" {
tableCellView.aTextField?.bind("value", toObject: tableCellView, withKeyPath: "objectValue.name", options: nil)
} else if tableColumn?.identifier == "raise column" {
tableCellView.aTextField?.bind("value", toObject: tableCellView, withKeyPath: "objectValue.raise", options: nil)
}
return tableCellView
}
这是我的子类NSTableCellView:
class MyTableCellView: NSTableCellView {
var aTextField: NSTextField?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
aTextField = NSTextField(frame: frameRect)
aTextField?.drawsBackground = false
aTextField?.bordered = false
self.addSubview(aTextField!)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
