2016-04-10 637 views
0

我尝试了许多不同的方法来创建具有自定义NSTableCellView的NSTableview,但我无法使其工作。问题是,我怎么能做到这一点?如何使用自定义视单元格创建NSTableview

这里是我试过的最后一件事:

enter image description here

而且全码::

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

    var cellIdentifier: String = "" 


    if tableColumn == tableView.tableColumns[0] { 

     cellIdentifier = "CellID" 

     if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self) as? MyTableCellView { 

      cell.identifier = cellIdentifier 
      // array is an array that contains NSView with layers with different colors 
      cell.myView = array[row] 
      return cell 
     } 
    } 
    return nil 
} 

enter image description here

添加标签后类的ViewController:NSViewController,NSTableViewDelegate,NSTableViewDataSource {

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableview.setDelegate(self) 
    tableview.setDataSource(self) 

    let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200)) 
    view.layer?.backgroundColor = NSColor.blueColor().CGColor 
    array.append(view) 
    let view2 = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200)) 
    view2.layer?.backgroundColor = NSColor.greenColor().CGColor 
    array.append(view2) 

    array2label.append("bu") 
    array2label.append("buu") 

    tableview.reloadData() 

    // Do any additional setup after loading the view. 
} 

override func viewWillAppear() { 


    //tableview.reloadData() 

    laView.layer?.backgroundColor = NSColor.greenColor().CGColor 
} 
@IBOutlet weak var laView: NSView! 

@IBOutlet weak var tableview: NSTableView! 

var array = [NSView]() 
var array2label = [String]()// = ["bu","buu"] 

func numberOfRowsInTableView(tableView: NSTableView) -> Int { 
    if (tableView.identifier == "Taula") { 

     return array.count 
     //return taulaGrafics.count 
    } else { 
     return 0 
    } 

} 

@IBOutlet weak var viewDeProva: NSView! 

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { 
    print ("Preparem la TableView") 
    var cellIdentifier: String = "" 


    if tableColumn == tableView.tableColumns[0] { 

     cellIdentifier = "CellID" 

     if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self) as? MyTableCellView { 
      print ("aqui") 
      print(array) 
      print(array2label) 

      cell.identifier = cellIdentifier 
      cell.myView = array[row] 
      cell.label.stringValue = array2label[row] 

      return cell 
     } 
    } 
    return nil 
} 

@IBAction func afegir(sender: NSButton) { 
    let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200)) 
    view.layer?.backgroundColor = NSColor.yellowColor().CGColor 
    array.append(view) 
    array2label.append("buLabel") 
    tableview.reloadData() 
} 
@IBAction func treure(sender: NSButton) { 
    array.removeLast() 
    tableview.reloadData() 
} 
} 

enter image description here

if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self) as? MyTableCellView { 
      print ("aqui") 
      print(array) 
      print(array2label) 

      cell.identifier = cellIdentifier 
      cell.myView = array[row] 
      cell.myView.wantsLayer = true 
      cell.label.stringValue = array2label[row] 

      return cell 
     } 
+0

你有没有加入您的自定义单元格故事板中的tableView?你有没有设置标识符? – mangerlahn

+0

@Max是的,我确实在问题 –

+0

上看到新图像由于cellView除了myView以外什么都不做,所以错误可能在那里。要测试它,只需在cellView中添加一个标签或按钮,以检查它是否已加载。 – mangerlahn

回答

0

随着这改变了视图显示,预计的颜色:

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    print("DidLoad") 
    tableview.setDelegate(self) 
    tableview.setDataSource(self) 
} 

override func viewWillAppear() { 
    print("WillAppear") 
    array.append(NSColor.blueColor().CGColor) 
    array2label.append("buIni") 
    tableview.reloadData() 
    laView.layer?.backgroundColor = NSColor.greenColor().CGColor 
} 

@IBOutlet weak var laView: NSView! 

@IBOutlet weak var tableview: NSTableView! 

var array = [CGColor]() 
var array2label = [String]() 

func numberOfRowsInTableView(tableView: NSTableView) -> Int { 
    if (tableView.identifier == "Taula") { 

     return array.count 
     //return taulaGrafics.count 
    } else { 
     return 0 
    } 

} 

@IBOutlet weak var viewDeProva: NSView! 

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { 
    print ("Preparem la TableView") 
    var cellIdentifier: String = "" 


    if tableColumn == tableView.tableColumns[0] { 

     cellIdentifier = "CellID" 

     if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self) as? MyTableCellView { 
      print ("Here") 
      print(array) 
      print(array2label) 
      cell.identifier = cellIdentifier 
      cell.myView.layer?.backgroundColor = array[row] 
      cell.label.stringValue = array2label[row] 

      return cell 
     } 
    } 
    return nil 
} 

@IBAction func afegir(sender: NSButton) { 
    let color = NSColor.yellowColor().CGColor 
    array.append(color) 
    array2label.append("buLabel") 
    tableview.reloadData() 
} 
@IBAction func treure(sender: NSButton) { 
    array.removeLast() 
    array2label.removeLast() 
    tableview.reloadData() 
} 
} 

问题是NSTableViewCell内部的NSView不能被另一个视图替换,因为w你这样做的帽子正在改变原型单元格。因此,如果您替换视图,NSTableViewCell无法识别NewView图层(我不确定100%为什么)。看起来只是一些信息是共享的。

那么,我们如何将信息传递给tableview单元格?我们如何在那里显示一个图层?那么答案是,只需修改界面构建器或其子类中添加的原型单元格NSTableCellView(如我的情况,请参阅单元格下面的内容)。在函数中修改其内容,但不修改NSView!的内容。看到这段代码:

if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self) as? MyTableCellView { 

      cell.identifier = cellIdentifier // that is to identify the cell 
      cell.myView.layer?.backgroundColor = array[row] // see that the array contains CGCOLORS not NSViews 
      // The program was not showing the colors because the 
      // view copied doesn't copy its layer, or at least 
      // doesn't show it. 
      // Even though, if you say: *******---This is incorrect: 
      cell.myView = arrayOfNSViews[row] //the program will 
      // crash when removing 2 rows, myView = nil !!!!. 
      // That is because when you remove the item of the array 
      // somehow you are removing myView too, because it make a 
      // copy or a reference to it (Not sure what exactly). 
      // Here continues the correct program: ******--- 

      cell.label.stringValue = array2label[row] 

      return cell 
} 

也看到,在文本字段的情况下:cell.label.stringValue = array2label[row],更改文本框,而不是整个NSTextfield的字符串值。

所以大家记得并重复我的话:“我不会改变单元格的视图,只是它的属性”。我只花4天发现......

这里是NStableCellView承诺:

视图层次的
class MyTableCellView: NSTableCellView { 

    @IBOutlet weak var myView: NSView! 

    @IBOutlet weak var label: NSTextField! 

} 

一个图片:

enter image description here

相关问题