Posts

Post not yet marked as solved
6 Replies
1.4k Views
I am new to Xcode macOS and Swift. I searched far and wide to finda simple example for getting data values from a tableView.I figured out a simple example and am posting it here in hopes thatit will help others.The trick is getting the indexes from the table on screen andthen referencing your data source that is currently loaded in memoryusing 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 whosewrapped extension name is the same as your primary ViewController.The name of the extension files by convention is the name ofthe ViewController you are extending with an added plus sign '+' andfinally the name of what this code page does or what this code page is.In this case it would be something likeMainViewController+SimplePrintTableValuesExampleBut 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 beginningextension MainViewController { // -- Mandatory if your main view controller name is 'MainViewController'The rest of code is then wrapped in a single function - This is also mandatoryI am extending the function tableViewSelectionDidChange.Xcode populated the rest of that line.The output is in the console (debug area) at the bottom of yourXcode screen.The 'myDataArray' needs to be prepopulated first andthat 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)") } }
Posted Last updated
.
Post not yet marked as solved
0 Replies
837 Views
This is my first app to the mac app store. It appears to verify ok, however; the icon is not showing up in Transporter.I have uploaded many different versions and even submitted one temporarily just to see if that was the issue - but the icon never shows up.Any suggestions are appreciated.ThanksBen
Posted Last updated
.