8

I have setup a NSView with an embedded NSTableView.

enter image description here

I have tried to set an action for the NSTableViewCell to run when a change is made to the Table View Cell:

import Cocoa

class MyView: NSView
{
    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    @IBAction func valEntered2(sender: AnyObject)
    {
        Swift.print("valueEntered2")
    }
}

Although this approach has worked fine previously when using a NSViewController on a storyboard, when I compile this code it gives a warning:

Action 'valEntered2:' sent by 'Table View Cell' is connected to 'View', an invalid target (Objects inside view based table views they only be connected to the table view's delegate.)

So it seems that it doesn't like the action being in the the NSView so I have subclassed NSTableView so I can move it there instead:

class MyTable: NSTableView, NSTableViewDataSource, NSTableViewDelegate
{
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

    override init(frame frameRect: NSRect)
    {
        super.init(frame: frameRect)
    }

    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
        self.setDataSource(self)
        self.setDelegate(self)
    }

    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int
    {
        return 5
    }

    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
    {
        let cell: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView
        cell.textField!.stringValue = "Hello World"
        return cell
    }
}

I have setup the table in the view to use this class:

enter image description here

I would have expected this to be the delegate for this NSTableView and to be able to add the TableViewCell's action to MyTable but it refuses to create the action.

What steps have I missed?

iphaaw
  • 6,764
  • 11
  • 58
  • 83

1 Answers1

0

first of all you have to add a delegate in your tableView cell like

import Cocoa
protocol cellAction {
  func CellAction()
}

class MyView: NSView
{
var delegate:cellAction!
    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    @IBAction func valEntered2(sender: AnyObject)
    {
      delegate.CellAction()
        Swift.print("valueEntered2")
    }
}

than after that you have to add delegate to the table view

class MyTable: NSTableView, NSTableViewDataSource, NSTableViewDelegate,cellAction
{
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

    override init(frame frameRect: NSRect)
    {
        super.init(frame: frameRect)
    }

    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
        self.setDataSource(self)
        self.setDelegate(self)
    }

    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
    }

    func numberOfRowsInTableView(tableView: NSTableView) -> Int
    {
        return 5
    }

    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
    {
        let cell: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView
        cell.textField!.stringValue = "Hello World"
       cell.delegate = self
        return cell
    }
}

in that class you have to call a method in delegate

func CellAction(){
 //here you get the call back 
}
  • I have copied the changes into my code. The line cell.delegate = self gives value of type NSTableCellView has no member delegate. I'm still getting the same warning on compilation. – iphaaw May 20 '16 at 18:58
  • did you add "cellAction " delegate in uitableview class – Chaudhary Ankit Deshwal May 20 '16 at 19:06
  • The line cell.delegate = self gives value of type NSTableCellView has no member delegate. What could I be doing wrong? – iphaaw May 30 '16 at 18:09