String value of a Table view text cell

How do you get the value inside the text cell of a table view?

You can get its stringValue(). But given that you normally provide the contents, that’s often the long way home. What are you trying to do, exactly?

I have two table views and I populated the first table. The second table is to move items from the first over to the second. So when u select an item from the first table and press a button that item is removed from the first and put in the second.

I have tried using stringValue() but it I haven’t got it to work. I must be trying to get it in the wrong way, heres what I tried:


set theRowIndex to tableView's selectedRow()
set theRowTitle to theRowIndex's stringValue()

log theRowIndex
log theRowTitle


the log returns the Index number but not the title, and the console shows no error. Also if you try to add theRowTitle to the table view it states that theRowTitle is not defined.

Is there a better way to do this?

How? The way you populate the tables has a bearing on how you manipulate them.

I made a separate class for each table using the methods from part 2 of Craig’s tutorial. For the first table it is populated in the awakeFromNib() part of the first class. The second table remains unpopulated. The first table is referenced as a property in both classes.

You’re using a data source to populate the table, so you get the index of the row from the table, and then use that to get the value from the data used by the data source.

How would you go about doing that? Especially due to the fact there are two classes, one for each table.

You have your row index from this:

set theRowIndex to tableView’s selectedRow()

If your data is in a list called theData, you then want to get the value of item (theRowIndex as integer +1) of theData, delete it from theData, and add it to the list variable that holds the other table’s data.

Once you get the index of the selected row, the rest is just a vanilla AppleScript exercise.

I still have some problems getting this to work. First I’m using the NSMutableArray just like in Craig’s part 2 tutorial. So not sure how you delete items out of the array yet. But the main issue is getting the new data over to the second class so I can populate the second table with the new data. Here is what I have so far:


--IB Action when button is pressed, in the main class that populates first table.--
	on addData_(sender)
		log "Start"
		set theRowIndex to aTableView's selectedRow()
		log theRowIndex --works
		set theRow to theDataSource's objectAtIndex_(theRowIndex)
		log theRow --works but shows an array object and that causes an error if you try to convert to a string.
		
		SecondClass's addDataToTable_(theRow)
	end addData_

--Method in second class that should populate the second table.--
	on addDataToTable_(theNewData)
		set theData to {{theSecondTableColumn:theNewData}}
		theDataSource's addObjectsFromArray_(theData)
		
		secondTableView's reloadData()
		log "END"
	end addDataToTable_

Console returns:
Start
(index of the row)
(array object) example: {theFirstTableColumn = “This is a table cell”; }
END

Even if you "set theRow to “TEST”, just to see if the second table would populate, nothing happens. The console still returns “END” and no errors but the second table will not populate. If i create an IB action inside the second class and put in the same actions inside the second method above and change theNewData to “TEST” the table will populate the first cell with “TEST”. So I’m not sure how to even transfer the new data to the second class to populate the second table.

So I need to transfer the new data to the second class somehow and also get just the value inside the table cell so in {theFirstTableColumn = “This is a table cell”; } returned from theDataSource’s objectAtIndex_(theRowIndex) I want only to have “This is a table cell”

What you’re getting as theRow is not an array, it’s a record/dictionary. You need to extract the value from that, probably like this:

set theEntry to theDataSource's objectAtIndex_(theRowIndex)'s valueForKey_("theFirstTableColumn")
-- remove item
theDataSource's removeObjectAtIndex_(theRowIndex)

Thanks! That works fine. Now the only thing left to do is somehow figure out how to pass “theDataSource” array from one class file to the next to update both table views. For some reason when you call a method of another class and pass a variable to it, it doesn’t actually work as if you called the method inside the class the method is in…

You need to make sure the method names follow Cocoa conventions, and if you want to do AS stuff on what gets passed, you have to coerce at both ends.

Heres the code I have.


--this method works perfect if you change it to setNewData_(sender) and connect it to a button 
--but not if called from the second class
	on setNewData()
		log "Main class"
		set theRowIndex to aTableView's selectedRow() --if ran from second class this returns no error but does not assign the variable...
		set theRow to theDataSource's objectAtIndex_(theRowIndex)'s valueForKey_("theLibrary") --error stating theRowIndex is not defined
		log theRow
		
		theDataSource's removeObjectAtIndex_(theRowIndex)
		aTableView's reloadData()
	end setNewData_

--this is the method that runs first when you hit the button--
	on addTheData_(sender)
		log "secondClass"
		set theRowIndex to mainView's selectedRow()
		log theRowIndex
		
		--works up to this point

		mainTableClass's setNewData() --runs method of first class
		
		set theData to {{theQueue:theNewData}}
		theDataSource's addObjectsFromArray_(theData)
		
		
		queueView's reloadData()
	end addTheData_


so for some reason calling methods in another class does not seem to work correctly for me. Any way to get this to work?

Is aTableView in the second class connected as an outlet to the table?

No the second class uses mainView instead of aTableView

And mainView is connected.

In that case you can’t use it in the second class. Each class’s outlets are separate.