How to detect text in NSTableview?

145 Views Asked by At

I am trying to find out how to detect text in NSTableView. There are many ways to do it and I have tried it all without success. I am using macOS 10.13 and swift 4 and I am new to swift coding.

I want to detect text in targetInput and then use the text to fill other cells.

I have the following code: ViewController

import Cocoa
import Alamofire


class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSTextFieldDelegate {
var stocks = [StockInformation]()
@IBOutlet weak var tableView: NSTableView!


@IBOutlet weak var input: NSTextField!
@IBAction func additem(_ sender: Any)  {
    if !input.stringValue.isEmpty {
        let stock = StockInformation(symbol: input.stringValue)
        stock.GetStockInformation {
            let insertionIndex = IndexSet(integer: self.stocks.count)
            self.stocks.append(stock)
            self.tableView.insertRows(at: insertionIndex, withAnimation: .effectGap)
            self.input.stringValue = ""
        }
    }
}

@IBOutlet weak var targetInput: NSTextFieldCell!


func textDidChange(_ notification: Notification) {
    print("Its come here textDidChange")
    guard (notification.object as? NSTextFieldCell) != nil else { return }
    let numberOfCharatersInTextfield: Int = targetInput.accessibilityNumberOfCharacters()
    print("numberOfCharatersInTextfield = \(numberOfCharatersInTextfield)")
}


override func viewDidAppear() {
    tableView.reloadData()
}


override func viewDidLoad() {
    super.viewDidLoad()
}


 func numberOfRows(in tableView: NSTableView) -> Int{
    return stocks.count
}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
    return stocks[row]
}


public func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
    print("swipe")
    // left swipe
    if edge == .trailing {
        let deleteAction = NSTableViewRowAction(style: .destructive, title: "Delete", handler: { (rowAction, row) in
            // action code
            self.stocks.remove(at: row)
            tableView.removeRows(at: IndexSet(integer: row), withAnimation: .effectFade)
        })
        deleteAction.backgroundColor = NSColor.red
        return [deleteAction]
    }
    let archiveAction = NSTableViewRowAction(style: .regular, title: "Archive", handler: { (rowAction, row) in
        // action code
    })
    return [archiveAction]
}

  }

The table: enter image description here

0

There are 0 best solutions below