是否有可能纯粹在代码中创建基于视图的NSTableView? [英] Is it possible to create a view-based NSTableView purely in code?

查看:158
本文介绍了是否有可能纯粹在代码中创建基于视图的NSTableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在代码中创建了一个基于单元格的NSTableView。我想让单元格有点更有趣,我已经阅读,我需要创建一个基于视图的NSTableView。

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.

我已经教程了这是

我的UI的其余部分完全在代码中。我一直在为这个tableview做同样没有太多的运气。

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

这里是我如何定义TableView - 我需要停止注册Nib和我不是如何:

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!

推荐答案

-tableView(_:viewForTableColumn:row:)的实现应该看起来像这样:

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
}

在表包含数百行的情况下,Cocoa会尝试重用已创建的视图,但不再在屏幕上。此代码段的第一部分使用 NSTableView 方法查找此类视图。如果没有找到,您需要从头创建一个。

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.

如果您没有理由,您应该使用 NSTableCellView 的实例(或子类)你的观点。它不会为<$​​ c $ c> NSView 添加太多,但它的一个关键特性是它保留对视图表示的模型的引用(由 -tableView(_:objectValueForTableColumnRow:row:))。在这个例子中,我使用这个功能使用绑定设置文本字段的字符串值。

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.

另外要注意的是,你应该给你的视图一个标识符与您给予 NSTableColumn (视图将位于其中)的标识符匹配。这样做允许您的表视图使用上述可重用视图功能。

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.

这篇关于是否有可能纯粹在代码中创建基于视图的NSTableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆