在Swift中使用绑定以编程方式创建基于视图的NSTableView [英] Create view based NSTableView programmatically using Bindings in Swift

查看:101
本文介绍了在Swift中使用绑定以编程方式创建基于视图的NSTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Swift中的可可书,我被困在关于绑定的章节中。这本书使用笔尖文件,但我想以编程方式进行所有操作(因为我要加入一个不使用笔尖的团队)。该项目将创建一个包含2列的基于视图的表,并且该表的内容绑定到数组控制器的 arrangedObjects 。数组控制器的内容绑定到一个Employee对象数组(Employee具有2个属性,即名称和薪水)。

I am working through a Cocoa in Swift book and I am stuck in a chapter on Bindings. The book uses nib files but I want to do everything programmatically (since I am joining a team that does not use nibs). The project is to create a view based table with 2 columns and the content of the table is bound to the arrangedObjects of an array controller. The array controller's content is bound to an array of Employee objects (Employee has 2 properties viz. name and salary).

我能够以编程方式创建表,如下所示(一个滚动视图,一个表视图,2个表列):

I was able to create the table programmatically like below (one scroll view, one table view, 2 table columns):

let tableWidth = windowWidth! * 0.6
let tableHeight = windowHeight! * 0.8

scrollView = NSScrollView(frame: NSRect(x: windowWidth!*0.05, y: windowHeight!*0.08, width: tableWidth, height: tableHeight))

employeeTable = NSTableView(frame: NSRect(x: 0, y: 0, width: tableWidth, height: tableHeight))
employeeTable?.bind("content", toObject: (self.arrayController)!, withKeyPath: "arrangedObjects", options: nil)

nameColumn = NSTableColumn(identifier: "name column")
nameColumn?.width = tableWidth * 0.4
nameColumn?.headerCell.title = "Name"

raiseColumn = NSTableColumn(identifier: "raise column")
raiseColumn?.width = tableWidth * 0.6
raiseColumn?.headerCell.title = "Raise"

employeeTable?.addTableColumn(nameColumn!)
employeeTable?.addTableColumn(raiseColumn!)
employeeTable?.setDelegate(self)

scrollView?.documentView = employeeTable

如您所见,我不知道此表是基于单元格还是基于视图。我怎么知道我的表是基于什么的?由于本章是关于绑定的,所以没有使用委托或数据源方法,我也想这样做。

As you can see, I have no clue if this table is Cell-based or View-based. How can I tell what my table is based on? Since the chapter is on Bindings, no delegate or data source methods are being used and I would like to do the same.

下一个问题:
,该书使用NIB,并且可以随时访问其下的NSTableView,NSTableCellView和NSTextField。首先,将NSTableView的内容绑定到阵列控制器的rangedObjects。自从我自己在代码中创建tableView对象以来,我就可以通过编程方式完成此部分。然后,该书将NSTextField的值绑定到NSTableCellView的objectValue.name(名称是Employee对象的属性之一)。由于我没有在代码中创建这些NSTableCellView和NSTextField对象,因此该怎么做?有没有办法访问它们(假设我的表甚至是基于视图的表)?

Next question: Like I said, the book uses NIBs and has ready access to the NSTableView, NSTableCellView and the NSTextField under it. First, the NSTableView's content was bound to the array controller's arrangedObjects. I was able to do this part programmatically since I created the tableView object myself in code. The book then binds the NSTextField's value to the NSTableCellView's objectValue.name (name is one of the Employee object's properties). How do I do this part since I did not create these NSTableCellView and NSTextField objects in my code? Is there a way to access them (assuming my table is even View-based)?

推荐答案

我在回答自己的问题。请注意,我是一个初学者,不知道这是否是正确的处理方法。正如用户stevesilva在上述问题的注释中指出的那样,我必须实现委托方法 tableView:viewForTableColumn:row:以确保该表基于视图。在委托方法中,我尝试创建一个NSTableCellView并绑定了textField属性,但这没有用。我必须继承NSTableCellView,创建一个新的文本字段属性,然后绑定该属性。这就是我的代表最终的样子。

I am answering my own question. Note that I am a beginner and do not know if this is the right way to do things. As noted by user stevesilva in the comments of the above question, I had to implement the delegate method tableView:viewForTableColumn:row: to ensure that the table is view based. Within the delegate method I tried to create a NSTableCellView and bind the textField property but this did not work. I had to subclass NSTableCellView, create a new text field property and then bind that property. This is how my delegate eventually looked.

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let frameRect = NSRect(x: 0, y: 0, width: tableColumn!.width, height: 20)

    let tableCellView = MyTableCellView(frame: frameRect)

    if tableColumn?.identifier == "name column" {
        tableCellView.aTextField?.bind("value", toObject: tableCellView, withKeyPath: "objectValue.name", options: nil)
    } else if tableColumn?.identifier == "raise column" {
        tableCellView.aTextField?.bind("value", toObject: tableCellView, withKeyPath: "objectValue.raise", options: nil)
    }

    return tableCellView
}

这是我的子类NSTableCellView:

And this is my subclassed NSTableCellView:

class MyTableCellView: NSTableCellView {

    var aTextField: NSTextField?

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        aTextField = NSTextField(frame: frameRect)
        aTextField?.drawsBackground = false
        aTextField?.bordered = false
        self.addSubview(aTextField!)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

这篇关于在Swift中使用绑定以编程方式创建基于视图的NSTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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