如何在可可NSTableView中添加列? [英] How to add columns in Cocoa NSTableView?
问题描述
我想在NSTableView中显示数据。未指定列数(取决于数据,列数可以是1到??),所以我不能使用Interface Builder。
因此,我(用IB)将表初始化为1列,然后根据需要添加新列(然后删除不再需要的第0列)。对于每个添加的列,我提供一个唯一的标识符。到目前为止一切顺利。
我实现了 tableView(-:viewForTableColumn:row)函数,如下所示,但是 makeViewWithIdentifier 返回没有。怎么了?
如果我检测到 nil 返回值,则会创建一个带有正确标识符的 NSTableCellView 实例。但是数据未显示在表中。
I want to show data in NSTableView. The number of columns is unspecified (could be any from 1 to ?? depending on the data), so I can't use Interface Builder.
So I initialize (with IB) my table to 1 column, and thereafter add new columns as required (then remove the no-longer needed 0-th column). To each added column I provide a unique identifier. So far so good.
I implement the tableView(-:viewForTableColumn:row) function, as shown below, but the makeViewWithIdentifier returns nil. What's the matter ?
If I detect the nil return, I create an instance of NSTableCellView with the proper identifier. But the data do not show in the table. What could be wrong ?
下面的代码(删除了不必要的行):
The code is below (with unnecessary lines removed) :
import Cocoa
class MyViewController: NSViewController {
@IBOutlet weak var dataTable: NSTableView!
}
var donnees: DataFrame = DataFrame() // Some table-like data with unspecified number of columns
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
initData() // get the actual data
// Make the ad-hoc number of columns
if donnees.nbCol > 0 {
for k in 0..<donnees.nbCol {
let newColumn = NSTableColumn(identifier: idArray[k]) // idArray : [String] of unique identifiers
dataTable.addTableColumn(newColumn)
}
}
dataTable.removeTableColumn(dataTable.tableColumns[0]) // remove the original column, now unnecessary
}
}
extension MyViewController : NSTableViewDataSource {
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return self.donnees.nbRow
}
}
extension MyViewController : NSTableViewDelegate {
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let columnID = tableColumn!.identifier
var cellView: NSTableCellView
let cellViewTmp = tableView.makeViewWithIdentifier(columnID, owner: self)
if cellViewTmp == nil { // then create a new NSTableCellView instance
cellView = NSTableCellView(frame: NSRect(x: 0, y: 0, width: (tableColumn?.width)!, height: 20))
cellView.identifier = columnID
print("CellView créé pour id \(columnID) au rang \(row)")
} else {
cellView = cellViewTmp as! NSTableCellView
}
cellView.textField?.stringValue = "AAA"
return cellView
}
}
推荐答案
宾果!感谢Willeke,我重新编写了如下代码:
Bingo ! Thanks to Willeke I rewrote my code as follows :
var donnees: DataFrame = DataFrame() // Some table-like data with unspecified number of columns
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
initData() // get the actual data
self.setTableColumns() // and prepare the columns accordingly
}
func setTableColumns() {
// In Interface Builder I've prepared a cell view in the 0-th tableColumn and set the identifier of this NSTableColumn to "ModelCellView"
let myCellViewNib = dataTable.registeredNibsByIdentifier!["ModelCellView"] // I save the cellView's Nib
// Make the ad-hoc number of columns
if donnees.nbCol > 0 {
for k in 0..<donnees.nbCol {
let newColumn = NSTableColumn(identifier: idArray[k]) // idArray : [String] of unique identifiers
dataTable.addTableColumn(newColumn)
dataTable.registerNib(myCellViewNib, forIdentifier: newColumn.identifier) // I register the above Nib for the newly added tableColumn
}
dataTable.removeTableColumn(dataTable.tableColumns[0]) // remove the original column, now unnecessary
}
}
extension MyViewController : NSTableViewDataSource {
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return self.donnees.nbRow
}
extension MyViewController : NSTableViewDelegate {
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let columnID = tableColumn!.identifier
let cellView = tableView.makeViewWithIdentifier(columnID, owner: self) as! NSTableCellView
cellView.textField?.stringValue = "theActualCellData"
return cellView
}
它可以按预期工作。再次感谢Willeke。
And it works perfectly as intended. Again, thanks to Willeke.
这篇关于如何在可可NSTableView中添加列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!