2016-04-10 12 views
0

Ich habe viele verschiedene Möglichkeiten versucht, eine NSTableview mit benutzerdefinierten NSTableCellView zu erstellen, aber ich konnte es nicht funktionieren. Die Frage ist, wie kann ich das Ding machen?So erstellen Sie eine NSTableview mit benutzerdefinierten Viewcells

Hier ist das letzte, was ich versucht:

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

Nachdem ein Etikett hinzu:

enter image description here

Und den vollständigen Code:

Klasse 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

Haben Sie Ihre individuelle Zelle hinzugefügt die TableView im Storyboard? Hast du den Bezeichner gesetzt? – mangerlahn

+0

@Max Ja, ich sehe das neue Bild auf der Frage –

+0

Da das CellView nichts außer myView tut, könnte der Fehler dort sein. Um dies zu testen, fügen Sie der cellView eine Beschriftung oder Schaltfläche hinzu, nur um zu überprüfen, ob sie geladen ist. – mangerlahn

Antwort

0

Damit ändert sich die Ansicht, die die Farbe zeigen, dass zu erwarten ist:

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

Das Problem war, dass das NSView innerhalb der NSTableViewCell nicht durch eine andere Ansicht ersetzt werden kann, weil w Hast du das getan, ändert sich die Prototyp-Zelle. Wenn Sie also die Ansicht ersetzen, erkennt die NSTableViewCell diese NewView-Ebene nicht (ich bin mir nicht 100% sicher, warum). Sieht aus wie nur einige Informationen geteilt werden.

Also, wie können wir Informationen an die Tabellenansicht Zelle übergeben? Wie können wir dort eine Schicht zeigen? Nun, die Antwort ist, dass nur die Prototyp-Zelle NSTableCellView geändert wird, die auf dem Interface-Builder oder seiner Unterklasse hinzugefügt wurde (wie in meinem Fall, siehe die Zelle unten). Ändern Sie den Inhalt, aber nicht das NSView !, innerhalb der Funktion. Siehe in diesem Stück Code:

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 
} 

auch sehen, dass im Textfeld Fall: cell.label.stringValue = array2label[row], ändern Sie den String-Wert des Textfeldes, nicht die ganzen NSTextfield.

Also Jungs erinnern sich und wiederholen meine Worte: "Ich werde nicht die Sicht auf die Zelle ändern, nur ihre Eigenschaften". Ich gebe nur 4 Tagen zu finden, dass ...

Hier ist die NStableCellView versprochen:

class MyTableCellView: NSTableCellView { 

    @IBOutlet weak var myView: NSView! 

    @IBOutlet weak var label: NSTextField! 

} 

Ein Bild der Ansicht Hierarchie:

enter image description here

Verwandte Themen