2016-04-10 5 views
0

カスタムNSTableCellViewを使用してNSTableviewを作成する方法はさまざまでしたが、動作させることができませんでした。問題はどうしたらいいのですか?ラベルを追加した後カスタムビューセルを使用してNSTableviewを作成する方法

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

:ここ

は、私が試した最後のものである

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!の内容を変更しました。コードのこの部分で参照してください:cell.label.stringValue = array2label[row]、あなたがテキストフィールド全体ではなく、NSTextfieldの文字列値を変更します。

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 
} 

ものTextField場合にいることがわかります。

だから私の言葉を覚えて、繰り返します。「私はセルの見た目を変えるつもりはない」私はちょうどそれを見つけるために、4日間を過ごす...

ここNStableCellViewが約束されます。ビュー階層の

class MyTableCellView: NSTableCellView { 

    @IBOutlet weak var myView: NSView! 

    @IBOutlet weak var label: NSTextField! 

} 

一つの画像:

enter image description here

関連する問題