Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

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

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-based NSTableView.

I have been tutorials like this.

The rest of my UI is entirely in code. I have been trying to do the same for this tableview without much luck.

Here is how I am defining the TableView — I need to stop registering the Nib and I am not sure how:

     let nib = NSNib(nibNamed: "TransactionCellView", bundle: NSBundle.mainBundle())
        tableOfTransactions.registerNib(nib!, forIdentifier: "TransactionCellView")

        tableOfTransactions.headerView = nil

        tableOfTransactions.setDelegate(self)
        tableOfTransactions.setDataSource(self)
        tableOfTransactions.reloadData()

Here is my stub code for each cell:

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?{
        var testCell = NSView()
        testCell.frame = NSRect(x: 0, y: 0, width: 300, height: 200)
        return testCell
}

Any pointers or suggestions on how to achieve this would be much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...