0

I am new to OSX coding and coding a mac OS app.

How can I access the text from rows that the user has selected. In this particular case, I am allowing users to select multiple rows to delete from the database in one go and is why I need to retrieve the text that I'm using as keys in the database.

Working in the console screen, here is a simplified version of what I'm tring to do.

import Cocoa
import Foundation

extension MainViewController {

  func tableViewSelectionDidChange(_ notification: Notification) {

    let myColumn = 0                          // -- first column
    let myRow = tableViewFolders.selectedRow  // -- row int index clicked

    let myCell:String = "?"  // -- ?get row or cell as string and/or array

    print("myColumn: \(myColumn)")
    print("myRow: \(myRow)")
    print("myCell: \(myCell)")

  }
  • Is the key editable? – Willeke May 21 '20 at 00:44
  • If I understand you correctly, no. I'm just displaying data from a select statement into the tableView. When I click on rows, I will be building an array to later feed to a database update statment (which will delete from the database those records) and then reload the data from the database back into the table. At least thats what I had planned. – BBExploration May 21 '20 at 01:08
  • Get the data from the data model or data source instead of the table view. – Willeke May 21 '20 at 09:38
  • I don't understand. The changes must come from the user. The only place the user can see the data is in the table. I'll keep looking thanks for trying. – BBExploration May 21 '20 at 13:32
  • How does the table know which data to display? – Willeke May 21 '20 at 14:05
  • This depends on how you maintain the data source, there are many ways: Cocoa Bindings, “half”-Cocoa Bindings (via `objectValue(for`), `NSArrayController ` or `NSTableViewDataSource`. – vadian May 21 '20 at 20:00

3 Answers3

0

Here is the simplest form of an answer I was able to figure out. This is only one column but I see no reason it would not work for multi-column tables. You'd have to access each column element using a column index after retrieving the row.

In a nutshell, I have an in-memory array.
That array has been loaded into a tableView. Here is the code to get the rows that the user has hilited and do something with those rows. Here I am just printint it to the console.

import Cocoa
import Foundation

extension MainViewController {
    // -----------------------------------------------------------
    // -- Note: that myDataArray is loaded and in memory
    // -- that data is now showing inside of the tableView
    // -- now we want to do something with the rows the user
    // -- has selected.
    // -- also note: I'm only accessing a single column tableView
    // -----------------------------------------------------------

    func tableViewSelectionDidChange(_ notification: Notification) {

        // -----------------------------------------------------------
        // -- this is an array to store the selected rows data
        // -- in my case this is actually an instance array
        // -- in which I call .removeAll() on
        // -----------------------------------------------------------
        selectedRowsData[String] = []


        // -----------------------------------------------------------
        // -- get the set of indexes for the rows that are selected
        // -----------------------------------------------------------
        // -- myTableView.selectedRowIndexes 
        // --    this is an index set containing 
        // --    the indexes of the selected rows
        let selectedIndexSet = myTableView.selectedRowIndexes     


        // -----------------------------------------------------------
        // -- if your curious this is the quantity of rows selected
        // -- we will be looping through this set in a moment
        // -- selectedIndexSet returns '# indexes' literally
        // -- this index set contains something like ["2", "5", "9"]
        // -- etc...
        // -----------------------------------------------------------
        // print("selectedIndexSet: There are \(selectedIndexSet)")


        // -----------------------------------------------------------
        // -- loop through the index set and extract 
        // -- from the original data array "myDataArray"
        // -- the value that each row index points to
        // -----------------------------------------------------------
        for index in selectedIndexSet {  
            if index > -1 {
                // -----------------------------------------------------------
                // -- append the actual data to selectedRowsData
                // -- this will provide quoted string comma separated data 
                // -- ["dataA", "dataB", "more data here"]
                // -----------------------------------------------------------
                selectedRowsData.append(myDataArray.self[index])
            }
        }
        print("selectedRowsData \n \(selectedRowsData)")
    }
}
0
//get the NsTableView cell from index in OSX / macOS catalyst 

 let tableView = notification.object as! NSTableView

  let row = table.selectedRow 

  let column = table.tableColumnWithIdentifier("ColumnID")

// you can specified your column as well

guard  let cell  = tableView.view(atColumn: 0, row: selectedIndex, makeIfNecessary: true) as? YourCustomCell else { return }

// do whatever you want with your cell from selected Index 
israkul9
  • 1
  • 2
-1

For fetching a single row data you can use:

func tableViewSelectionDidChange(notification: NSNotification) {
    let table = notification.object as! NSTableView
    print(table.selectedRow);

    var row = table.selectedRow
    var column = table.tableColumnWithIdentifier("ID")
    var cell: AnyObject? = column?.dataCellForRow(row)
    print("Cell Value - \(cell!.stringValue)")
}
grow4gaurav
  • 3,145
  • 1
  • 11
  • 12
  • I can't seem to post a formatted statement in the comment section. So I'll just say that even working through the errors I could not get the above code to work. Output is 'nil'. I am new so I probably just not reading and converting it correctly. I'm using Catalina, Xcode 11.1 – BBExploration May 20 '20 at 21:37
  • `dataCellForRow` is deprecated and for use with cell-based table views. – Willeke May 21 '20 at 00:44