setting contents of table view to variable?

This probably sounds stupid, but whats the script for setting the contents of a NSTableView named i.e. “table” with a variable? the table is one column, and the variable is an array of text.

thanks,

ben

Model: iBook g4
Browser: Camino 1.0b2
Operating System: Mac OS X (10.4)

Hi,

The Table example without data source is really confusing, because you don’t know what is happening when you tell the table to update. Here’s a simple example:

property tableData : {}
property the_count : 0

on clicked theObject
set the_count to the_count + 1
set end of tableData to the_count
tell table view “table” of scroll view “scroll” of window “main” to update
end clicked

on cell value theObject row theRow table column tableColumn
set theValue to 0
if (count tableData) ≥ theRow then
set listValue to item theRow of tableData
set theID to name of tableColumn
if theID is “number” then – irrelevent here
set theValue to listValue
end if
– our tableData is just a list of numbers
end if
return theValue
end cell value

on number of rows theObject
return (count tableData)
end number of rows

In IB, I added a table view “table” with one column and a button “button”.

To the button is connected to the clicked handler.

The table view is connected to the ‘cell value’ and ‘number of rows’ handlers.

When the user clicks the button, the clicked handler is called. The value of the_count is incremented and the value added to the list tableData. The value of tableData is:

{1}

at that point. Then when you table view is told to update, the table calls the ‘number’ of rows’ handler and the table adds a row, because the number of items in the tableData list is greater then the number of rows at that time. Then the table calls the ‘cell value’ handler with parameters values for row number and column reference. Here there is only one column, so this handler is only called once. If there were five columns, then this handler would automatically be called five times. Anyway, the value of theValue is taken from the list tableData and returned to the table view where the cell value is updated.

I think this is how it goes for the most part. Hope this clears things up.

gl,

could this work with, say, a variable set to the playlists in iTunes? How?

Model: iBook g4
Browser: Camino 1.0b2
Operating System: Mac OS X (10.4)

It’s easily modifiable. Say I wanted just a one column list of tracks of the currently selected playlist.

property track_names : {}

on clicked theObject
tell application “iTunes”
set bw to (first browser window)
set sel_pl to (view of bw) – selected playlist
set track_names to (name of every track of sel_pl)
end tell
tell table view “table” of scroll view “scroll” of window “main” to update
end clicked

on cell value theObject row theRow table column tableColumn
set theValue to “”
if (count track_names) ≥ theRow then
set listValue to item theRow of track_names
end if
return listValue
end cell value

on number of rows theObject
return (count track_names)
end number of rows

Note that there’s a lot more work if you want more than just the track names. Instead of a list of names you would use a list of lists or records. I think I would go with a list of records and list the artists also. There may be more than one track with the same name. Or maybe even add the album. Different albums have different versions.

It’s like you’re creating a machine that the table (program) may use when you tell it to update.

Editted: oops, this line:
set theValue to “”
should be:
set listValue to “”
Although it doesn’t matter that much here, only when you want to delete items from the table.

gl,