以编程方式添加到NSTableView的列在Delegate中无法识别

时间:2018-04-28 15:24:35

标签: swift cocoa nstableview tablecolumn

我可能会在一杯水中迷失,但我似乎无法在NSTableView中添加列,然后在NSTableViewDelegate中识别这些列。我在IB中创建了一个包含一列的表,并为该列提供了一个字符串标识符。我在视图控制器中添加了其他列:

 override func viewDidLoad() {
        super.viewDidLoad()

        for columnIndex in 0..<blotter!.singleOutput[0].parameter.count  {

            let tmpParam = blotter!.singleOutput[0].parameter[columnIndex]
            let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: tmpParam.columnID))
            column.title = tmpParam.label
            column.width = CGFloat(80)
            column.minWidth = CGFloat(40)
            column.maxWidth = CGFloat(120)
            blotterOutputTable.addTableColumn(column)

            }

        blotterOutputTable.delegate = self
        blotterOutputTable.dataSource = self
        blotterOutputTable.target = self

        blotterOutputTable.reloadData()

    }

NSTableViewDataSource返回正确的行数。我遇到的问题是在NSTableViewDelegate:

extension OutputsViewController: NSTableViewDelegate {

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

        var text: String = ""
        var cellIdentifier: String = ""

        guard let item = blotter?.singleOutput[row] else {   return nil  }

        // 1. LABELS COLUMN
        // ================

        if tableColumn?.identifier.rawValue == "dealColumn" {

            let myParameter = item.parameter.index(where: {$0.columnID == "BBTickColumn"})

            text =  item.parameter[myParameter!].value as! String               
            cellIdentifier = "dealColumn"

            if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: nil) as? NSTableCellView {

                cell.textField?.stringValue = text                  
                return cell
                }

            else {    return nil  }

            }   // END OF LABLES COLUMN (FIRST ONE)




        else {     // THIS IS WHERE THE PROBLEM IS

            let myParameter = item.parameter.index(where: {$0.columnID  ==  tableColumn?.identifier.rawValue } )
            let (_, valueAsText) = item.parameter[myParameter!].interfaceItems()
            text = valueAsText

            cellIdentifier = item.parameter[myParameter!].columnID


            if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: nil) as? NSTableCellView {

                cell.textField?.stringValue = text
                return cell
                }

            else {    return nil  }  // DEBUGGER PARAMETER ARE FROM HERE

            }      
        }

}

第一列是我在IB中使用其标识符创建的列。这样可行。我遇到的问题是在else语句中(不检查列标识符)。下面是在单元格创建失败后停止程序时在调试器窗口中看到的参数

tableView   NSTableView 0x000000010ebf9df0
tableColumn NSTableColumn?  0x0000600000895770
row Int 0
self    DataBaseManager.OutputsViewController   0x0000600000102b50
text    String  "FLAT"  
cellIdentifier  String  "directionColumn"   
item    DataBaseManager.BlotterOutputs  0x000060000002c240
myParameter Array.Index?    0
valueAsText String  "FLAT"  
cell    (null)  (null)  (null)
tableColumn NSTableColumn?  0x0000600000895770
tableColumn?.identifier NSUserInterfaceItemIdentifier?  some
_rawValue   _NSContiguousString "directionColumn"   0x000060000104d200
Swift._SwiftNativeNSString  _SwiftNativeNSString    
_core   _StringCore 

您可以看到cellIdentifier和tableColumn?.identifier.rawvalue是相同的字符串(应该是这样)。我无法理解为什么没有创建单元格。我们欢迎任何帮助,如果不清楚,请告诉我。感谢

1 个答案:

答案 0 :(得分:0)

必须按照以下代码注册笔尖标识符:

import Cocoa

class MultiColumnTable: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    var list = [[String]](), header=[String]()
    var tableView : NSTableView? = nil
    var nColumns : Int = 0

    func genID(col : Int) -> NSUserInterfaceItemIdentifier { // generate column ID
        return NSUserInterfaceItemIdentifier(rawValue: String(format: "Col%d", col))
    }

    func setContent(header: [String], list : [[String]]) {
        self.header = header
        self.list = list
        self.nColumns = list[0].count

        if tableView != nil {
            tableView?.reloadData()
        }
    }

    func numberOfRows(in tableView: NSTableView) -> Int {

        func createColumns() {
            func addColumn(col:Int, header:String) {
                let tableColumn = NSTableColumn(identifier: genID(col: col))
                tableColumn.headerCell.title = header
                self.tableView!.addTableColumn(tableColumn)
            }

            // create columns and register them in NIB
            // IB: tableColumn[0] identifier ( NSTableColumn to "Col0" )

            if let myCellViewNib = tableView.registeredNibsByIdentifier![NSUserInterfaceItemIdentifier(rawValue: "Col0")] {

                for col in 0..<nColumns { // table should have 1 col in IB w/Ident 'Col0'
                    addColumn(col: col, header: header[col])
                    tableView.register(myCellViewNib, forIdentifier:  genID(col: col))  // register the above Nib for the newly added tableColumn
                }
                tableView.removeTableColumn(tableView.tableColumns[0])  // remove the original Col0
            }
        }

        self.tableView = tableView
        createColumns()

        return list.count
    }

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

        let column = tableView.tableColumns.firstIndex(of: tableColumn!)!
        tableColumn?.headerCell.title=header[column];


        if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: self) as? NSTableCellView {

            cell.textField?.stringValue = list[row][column]
            cell.textField?.textColor = NSColor.blue
            return cell
        }
        return nil
    }

    func tableViewSelectionDidChange(_ notification: Notification) {
    }
}