Your implementation of -tableView(_:viewForTableColumn:row:)
should look something like this:
func tableView(tableView: NSTableView,
viewForTableColumn
tableColumn: NSTableColumn?,
row: Int) -> NSView? {
var retval: NSView?
if let spareView = tableView.makeViewWithIdentifier("CodeCreatedTableCellView",
owner: self) as? NSTableCellView {
// We can use an old cell - no need to do anything.
retval = spareView
} else {
// Create a text field for the cell
let textField = NSTextField()
textField.backgroundColor = NSColor.clearColor()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.bordered = false
textField.controlSize = NSControlSize.SmallControlSize
// Create a cell
let newCell = NSTableCellView()
newCell.identifier = "CodeCreatedTableCellView"
newCell.addSubview(textField)
newCell.textField = textField
// Constrain the text field within the cell
newCell.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|[textField]|",
options: [],
metrics: nil,
views: ["textField" : textField]))
newCell.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("V:|[textField]|",
options: [],
metrics: nil,
views: ["textField" : textField]))
textField.bind(NSValueBinding,
toObject: newCell,
withKeyPath: "objectValue",
options: nil)
retval = newCell
}
return retval
}
In the case where your table contains hundreds of rows, Cocoa will attempt to reuse views that have already been created, but are no longer on screen. The first part of this snippet uses an NSTableView
method to look for such a view. If none is found, you need to create one from scratch.
If you've got no reason not to, you should use an instance (or subclass) of NSTableCellView
as your view. It doesn't add much to NSView
, but one of its key features is that it retains a reference to the model that the view represents (set by -tableView(_:objectValueForTableColumnRow:row:)
). In this example I've used this feature to set the string value of the text field using bindings.
The other thing to note is that you should give your view an identifier that matches the identifier that you gave to the NSTableColumn
in which the view will sit. Doing so allows your table view to make use of the reusable-view feature discussed above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…