How to add columns in Cocoa NSTableView?

后端 未结 1 1374
小鲜肉
小鲜肉 2020-12-20 09:25

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 initi

1条回答
  •  难免孤独
    2020-12-20 09:45

    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.. 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
    }  
    

    And it works perfectly as intended. Again, thanks to Willeke.

    0 讨论(0)
提交回复
热议问题