Is it possible to create a view-based NSTableView purely in code?

后端 未结 2 997
闹比i
闹比i 2020-12-10 02:19

I have successfully created a cell-based NSTableView purely in code. I would like to make the cells a little more interesting and I have read that I need to create a view-ba

2条回答
  •  悲&欢浪女
    2020-12-10 02:48

    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.

提交回复
热议问题