在Swift中使用绑定以编程方式创建基于视图的NSTableView

发布于 2021-01-31 23:31:42

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

我可以像下面这样以编程方式创建表(一个滚动视图,一个表视图,2个表列):

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

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

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

关注者
0
被浏览
184
1 个回答
  • 面试哥
    面试哥 2021-01-31
    为面试而生,有面试问题,就找面试哥。

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

    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:

    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")
        }
    
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看