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