How to add an IBAction to a macOS tableView Cell

I have a macOS tableview I have a textfield as a column along with an IBAction code to capture the data from this field. I wanted to add continuous spell check for this column . So, I decided to change the column from a TextField to a ScrollView to take advantage of the continuous spell check. Using Xcode 14 I changed the Cell to a custom Cell. The custom cell has a scrollview object as the object because I want to take advantage of the built in continuous spell checking instead of using a textfield. All is well with this implementation until I tried to add a IBAction code for when I enter text in the scrollview/textView field.

There does not seem to be a way to capture the text? Am I trying to do something that can't be done or is there a way to add continuous spell check to a textField?

Accepted Reply

To get access from the VC, you should, in the VC:

  • create an IBOutlet for the tableView (not its container ScrollView)
  • get a reference to the cell, with something like (adapt row and column values)
     guard let cell = tableView.view(atColumn: 0, row: 0, makeIfNecessary: true) as? CustomTableCell else { return }
  • then use the cell to access any component you need:
     let text = cell.testTextView.textStorage?.string

Replies

All is well with this implementation until I tried to add a IBAction code 

What do you get ? What did you expect ?

Could you tell where you added the IBAction and show the code ? ANd show also a capture of the storyboard structure for the table cell ?

Have you declared the class as adhering to UITextFieldDelegate ?

Note: you don't enter text in the scrollView, only in the TextField. A TextView would also provide the same scrolling capability…

import Cocoa

class CustomTableCell: NSTableCellView {
    @IBOutlet weak var userNameLabel: NSTextField!
    @IBOutlet weak var roleLabel: NSTextField!
    @IBOutlet var testTextView: NSTextView!

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // Drawing code here.
    }
    @IBAction func emailButton(_ sender: Any) {
        // Email user
    }
}

I was first trying to add a @IBAction for a scrollview and when I control drag the scrollview to the CustomTableCell file I could not create and @IBAction. It is like having a scrollview object inside a scrollable table view is not allowed. So, I was not able to create an action to get the contents of the scrollview in the ViewController code. Much less creating code to pass to the viewController the contents of the scrollView text entered.

I sent a offline demo project to illustrate what I am saying.

Thanks for your help on this.

To get access from the VC, you should, in the VC:

  • create an IBOutlet for the tableView (not its container ScrollView)
  • get a reference to the cell, with something like (adapt row and column values)
     guard let cell = tableView.view(atColumn: 0, row: 0, makeIfNecessary: true) as? CustomTableCell else { return }
  • then use the cell to access any component you need:
     let text = cell.testTextView.textStorage?.string

Thanks Claude, could not have done it without your help.