I am new to Xcode macOS and Swift. I searched far and wide to find
a simple example for getting data values from a tableView.
I figured out a simple example and am posting it here in hopes that
it will help others.
The trick is getting the indexes from the table on screen and
then referencing your data source that is currently loaded in memory
using the indexes from the table.
The actual code is less than ten lines. Explaining it is an entire page.
This does use an extension which is a Swift file whose
wrapped extension name is the same as your primary ViewController.
The name of the extension files by convention is the name of
the ViewController you are extending with an added plus sign '+' and
finally the name of what this code page does or what this code page is.
In this case it would be something like
MainViewController+SimplePrintTableValuesExample
But it is not mandatory. You could call it 'beachBall. if you like.
What is mandatory is the extension inside the code.
My primary code page ViewController name is "MainViewController"
That is why this code is wrapped in the line beginning
extension MainViewController { // -- Mandatory if your main view controller name is 'MainViewController'
The rest of code is then wrapped in a single function - This is also mandatory
I am extending the function tableViewSelectionDidChange.
Xcode populated the rest of that line.
The output is in the console (debug area) at the bottom of your
Xcode screen.
The 'myDataArray' needs to be prepopulated first and
that array needs to already be in your tableView.
Then this code will run.
This is for macOS.
// -----------------------------------------------------------
// -- Getting data values from a tableView
// -----------------------------------------------------------
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 like this
// -- myDataArray.removeAll()
// -----------------------------------------------------------
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.
// -- I will be looping through this set in a moment.
// -- SelectedIndexSet returns '# indexes' literally.
// -- This index set contains something like ["2", "5", "9"]
// -- uncomment the print line to see the number of
// -- highlited rows
// -- etc...
// -----------------------------------------------------------
// print("selectedIndexSet: There are \(selectedIndexSet)")
// -----------------------------------------------------------
// -- Now 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 {
// -- This line is here in case no rows are highlited.
// -- If no rows are highlited index will be -1
// -- If index has a value less than zero this routine
// -- will crash.
// -----------------------------------------------------------
if index > -1 {
// -----------------------------------------------------------
// -- Append the actual data to selectedRowsData array
// -- This will provide quoted string, comma separated values.
// -- ["dataA", "dataB", "more data here"]
// -----------------------------------------------------------
selectedRowsData.append(myDataArray.self[index])
}
}
print("selectedRowsData \n \(selectedRowsData)")
}
}