简体   繁体   中英

get NSTableView cell values

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)")

  }

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)")
    }
}
//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 

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)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM