我在this之后以编程方式(仅限代码,没有XIB)构建了一个带有单个基于视图的NSTableView的窗口,该窗口由 swift中的 Array 支持即可。 我反而希望用 NSArrayController 来支持它,所以只需通过:
var controller = NSArrayController()
并在需要时使用其中一种正确的方法修改其内容:
controller.addObject(item)
controller.removeObjectAtArrangedObjectIndex(index)
controller.insertObject(item, atArrangedObjectIndex: atIndex)
并将其绑定:
tableView.bind(NSContentBinding, toObject: controller, withKeyPath: "arrangedObjects", options: nil)
tableView.bind(NSSelectionIndexesBinding, toObject: controller, withKeyPath:"selectionIndexes", options: nil)
tableView.bind(NSSortDescriptorsBinding, toObject: controller, withKeyPath: "sortDescriptors", options: nil)
在委托中创建单元格视图:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
// ... erased code for finding cellIdentifier and text..
view = tableView.makeViewWithIdentifier(cellIdentifier, owner: nil) as? PLTextCellView
if view == nil {
print("new view for text: "+text)
view = NSTextField()
view!.identifier = cellIdentifier
}
view!.stringValue = text
return view
}
添加/删除和项目数量正常工作但对象ID在单元格中显示在'<'之间。和'>',而不是我指定为 text 的值。这在 NSArrayController 之前工作正常。我需要做什么?
注意:
This问题提供了一个-incomplete-答案,其中出现An instance of ...item... was deallocated while key value observers were still registered with it.
错误。
我也根据this回答绑定了我的表,该回答仅指出了手动绑定单元格的必要性。我在问怎么样?