Trouble with DataView

Hi

I’ve been trying to experiment with using an NSDataView in a test application. Even after studying the Applescript documentation and the example apps I’m still having no sucess getting even the most basic example to work. I’m just not getting the data source right. Is there a good tutorial on using Data Views and data sources available?

Thanks… Edd

What’s a data view? Do you mean a Table View?

First you must set up the data source. This is typically done in an “awake from nib” handler attached to the table view itself. That way once you initialize the data source you can just set it to theObject. Make sure the name you give to each new data column is identical to that which you give to each column in the table view.

	(* Create the table's data source *)
	set theDataSource to make new data source at end of data sources with properties {name:"theTableViewData"}
		
	make new data column at end of data columns of theDataSource with properties {name:"col_1"}
	make new data column at end of data columns of theDataSource with properties {name:"col_2"}
		
	(* Attribute the data source to the table view *)
	set data source of theObject to theDataSource

Then to manage the data source, you add or delete items to the data source, and then tell the table view it’s ok to update, and it updates it’s contents automatically.

	(* Get the table's data source *)
	set theDataSource to data source of table view "tableView" of scroll view "scrollView" of window "window"

	(* Stop automatic updating of the view *)
	set update views of theDataSource to false
			
	(* Add a data row and it's contents *)
	tell theDataSource
		set theRow to make new data row at the end of the data rows
				
		set contents of data cell "col_1" of theRow to "Test"
		set contents of data cell "col_2" of theRow to "Another Test"	
	end tell
			
	(* Display the new contents *)
	set update views of theDataSource to true

j

Edit: I thought it hadn’t worked, but I had a typo.

I could have sworn I did everything here, but it seems to have worked, so thanks again!

Edd

These two SETs seem to conflict. Or is the new data source stuffed into the table view’s data source by the first SET command?

The first is essentially the initialization of the data source, which is intended to occur only once. As I said previously, it is usually used in the ‘awake from nib’ handler of the table view. It creates a new generic data source in the application, and then tells the table view to “stuff” it into it’s data source. :wink:

The second code is used somewhere else, in another handler or subroutine. It goes to the table view specified, and asks it for a reference to it’s current data source. The two variables “theDataSource” just happen to have the same name. They can have the same name or you can make them different. Only if you have a property variable saving a reference to the data source is it important what their names are, and only because you would want to make sure that when you set “theDataSource” you are setting it to a value that you are sure you want. Regular variables are by default local, and lose their value once the handler they are in completes it’s execution. So once the awake from nib handler is done executing, “theDataSource” becomes nothing. Likewise, when the other code is called, “theDataSource” gets a value temporarily again, and then is released when the handler is finished.

So, they are not conflicting under most circumstances. One is called once, and then is forgotten. The second can be called repeatedly, but only uses that reference temporarily. The variable (unless implicitly used as a property/global variable) always evaporates after it’s handler is executed.

Hope that clarifies…
j