MacOs - NSTableView, editing single cell

Using Swift, I have an NSTableView with four columns. Two of the columns have cells that I want the user to be able to edit.

In my Storyboard I have set the NSTextFieldCells to be

Behaviour: Editable Action: Sent on End Editing

I have created an @IBAction function to receive the action from the NSTextFieldCell but it never fires.

Everything about the NSTableView appears to work as expected – it displays the values that it should, however when I edit the editable cells the action never fires (and the cell values do not change, of course.)

What am I overlooking?

Thanks

Answered by OOPer in 687577022

I'm using a cell based NSTableView.

You should better read this old article:

Programmatically Editing Data in an NSCell-Based Table

Seems you need to implement tableView(_:setObjectValue:for:row:) in your NSTableViewDataSource:

    func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) {
        guard let stringValue = object as? String else {
            return
        }
        //Check `tableColumn` in a real implementation...
        print("cell was edited")
        print(stringValue)
        infoForTableView[row] = stringValue
    }

Generally, NSCell-based table view is sort of an older part of NSTableView, it is simpler in many simple cases, but less customizable.

Did you define the delegate (and dataSource) for the tableView ? You can do this in IB.

Could you try:

@IBAction func cellWasEdited(_ sender : NSTextField) { 
    print("cell was edited") 
}

Of course, first disconnect the IBAction, modify and reconnect. And do a Clean Build Folder.

And the class conforms to NSTextFieldDelegate.

Here is a case:

class DataEditWindowController: NSWindowController, NSWindowDelegate, NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate {

I do not understand where you defined the action as Editable Action: Sent on End Editing. I do not find Sent on End Editing for MacOS (exists on iOS).

Here is my setup:

I'm using a cell based NSTableView.

Posting code in the comment area gives weird formatting…

Here it is properly edited:

import Cocoa

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource { 
    var infoForTableView : [String] = ["one", "two", "three", "four", "five"]
     
     override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
     }
     
     override var representedObject: Any? {
          didSet {
               // Update the view, if already loaded.
          }
     }
     
     @IBAction func cellWasEdited(_ sender: NSTextFieldCell) {
          print("cell was edited")
          print(sender.stringValue)
     }
     
     func numberOfRows(in tableView: NSTableView) -> Int {
          return infoForTableView.count
     }
     
     func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
          return infoForTableView[row]
     }
     
}

I wonder if you have to set some binding ? Looked here but did not find the answer for cell-based: h t t p s : / / forums.bignerdranch.com/t/data-source-challenge-making-table-view-editable/7330/10

Accepted Answer

I'm using a cell based NSTableView.

You should better read this old article:

Programmatically Editing Data in an NSCell-Based Table

Seems you need to implement tableView(_:setObjectValue:for:row:) in your NSTableViewDataSource:

    func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) {
        guard let stringValue = object as? String else {
            return
        }
        //Check `tableColumn` in a real implementation...
        print("cell was edited")
        print(stringValue)
        infoForTableView[row] = stringValue
    }

Generally, NSCell-based table view is sort of an older part of NSTableView, it is simpler in many simple cases, but less customizable.

MacOs - NSTableView, editing single cell
 
 
Q