get value of tableview items from array controller

I’ll mess around with this. A simple record would be far nicer to use than the array language. . The values I need to change are in table 2 though and that is the one bound to table 1’s selection. It seems a bit trickier - because I can’t unbind table 2 to make it available to script.

And the prefs (all 24 of them!) could be done with selectionChanged but that means they need their own array somehow linked to the table’s array (scary.)

You can see the code idea I am using at

http://www.cocoadevcentral.com/articles/000080.php

which I have seen in many places as the usual way for this kind of binding of tables…

Now - I could go back to the old way using glue code for the two tables with datasource outlets and then have tables available for binding but that involves way more code and all the datasource handlers etc…

But I really would like to find a simpler way. in the end if I can get access to set the table values right, it still may be less code for me in the ned. Maybe if you post your example - and if you can do it with two tables linked my way, let me know. I’m no expert here!

Thanks Shane, Rob

Try something like this:

Have a property in your AppDelegate called theData, and set it to a list of records like:

	set my theData to {{mailBox:"Inbox", emails:{{firstName:"Ray", lastName:"Robertson"}, {firstName:"Shane", lastName:"Stanley"}}}, {mailBox:"Outbox", emails:{{firstName:"Jane", lastName:"Smith"}, {firstName:"Paula", lastName:"Ramsay"}}}}

Now add an Array Controller, call it First, and bind its Content Array to the AppDelegate, with Model Key Path of theData.

In your first table, bind the first column’s Value to First, arrangedObjects, MKP mailBox.

Now add a second Array Controller, call it Second, and bind its Content Array to First, Controller Key selection, MKP emails.

Finally, bind the Value of the first column of your second table to Second, arrangedObjects, MKP firstName. Bind the second similarly to lastName.

Now you can use the remove: method of the controllers, and you can add by modifying theData.

A tip for bindings stuff: do one at a time and test after each; it makes trouble-shooting so much easier.

Shane,

I gave it a try. I got the first table working Ok though if you edit rows they won’t retain the new values when you remove an item from the row.

Adding table 2 works also but is more buggy. When adding a row the first table gets deselected and you don’t see the new row. Same issue with editing rows and sometimes the rows get added instead of removed!

Something isn’t wired right.

I’ll play some more with it.

Rob

Make sure Handles Content as Compound Value is selected in the array controller’s binding.

That seems to have fixed the non-retaining issue though now, when you remove a row from either table, you can no longer add new rows to table 2!

I keep going around with this. It seems close to working but I also had to use

aTableView's selectRow_byExtendingSelection_(theoldRowIndex, false)

to get the table 1 to select the correct row after adding rows to table two.

If it can work in a non buggy way it will be good. I still prefer using the classes I have set up for handling the array controllers since it is very solid and requires no workarounds.

Yet if this can work, it will be a nice simple solution, especially for those with no OBJ-C in their proj.

I’ll experiment some more,

Rob

actually with this selected for table 2, the rows add after a remove!

With it selected you can add rows but when you delete some and add a row again , it returns to the all rows displayed before the first deletion.

Rob

How are you adding and removing?

Hi Shane,

I use

  
property theData:missing value  

on addSet_(sender)

		set my theData to my theData & {{backupset:"newBackupset", sourcefiles:{{fileicon:"",sourcefile:"somefile", filesize:"---"}}}}
		
end addSet_


on awakeFromNib()
		
		set my theData to {{backupset:"newBackupset", sourcefiles:{{fileicon:"a", sourcefile:"somefile", filesize:"---"}}}, {backupset:"newBackupset", sourcefiles:{{fileicon:"b", sourcefile:"anothersomefile", filesize:"---"}}}}

	end awakeFromNib


for the add and the built in “remove” connected through IB.

I started fresh again and copied exactly all the same settings you had in the sample and went back to just one table and it does the same thing: When you edit rows and then do “remove”, the rows return to their original values. It seems better than before otherwsie. Worse trouble begins when I hook up the second table.

Rob

Try reversing that to:

set my theData to {{backupset:“newBackupset”, sourcefiles:{{fileicon:“”,sourcefile:“somefile”, filesize:“—”}}}} & my theData

You’ll see I mention that in the sample I posted about. I suspect it forces coercion of theData from being just a pointer to a record.

I tried that and it has the same problem. I changed it because I wanted the new set to appear at the end of the list.

But still can’t edit the fields and retain their values. I must be missing something because it seems to edit and retain in your sample proj.

Rob

All I can suggest is that you play with the bindings settings – they can be tricky.

I just downloaded a fresh copy of your florida app and it does the same thing!

Edit some of the names and then try to delete a row. The values go back to the original values. Oddly the last time I tried this with your app it seemed to retain values…

Rob

Guess who missed his own advice: make sure Handles Content as Compound Value is checked for the Array Controller’s binding!

Ok - it works. I think.

The two tables are now in sync without any weird behavior. I had to mess around with the array controller settings for the bindings and the attributes to get it right. I’ll post the result if it all looks good tomorrow.

Thanks Shane. If this works it will have saved me many lines of code.

Rob

Shane,

I thought I was out of the woods. The two tables are working perfectly in snyc.

In my app though, table 2 should be empty (no placeholders). Table one can have a placeholder representing the first set fine.

I have spent many hours trying to make this work.

First I tried initializing with this:

set my theData to {{backupset:"newset", sourcefiles:""}}

but weird things happen when you try to add new files to the list.

set sourcefiles of item actualSelectedRow of theOriginalData to :{{ filename:newFilename, filepath:newFilepath}}}

}

It adds the new row but then all the sets show the same new row in their list. The bugs go on…

double curly braces instead of “” in sourcefiles doesn’t work at all.

I also tried using the placeholder text and replacing it with the new data, which works but then there are other bugs.

Should we be using NSMutableArray instance instead of a plain record to send to OBJ-c array controllers?

Stumped, Rob

That’s what I’d expect to work, and does here in my (limited) testing.

Where does theOriginalData fit into the scheme of things?

I was using “my thedata” referring to the variable bound to the array controllers content. I tried setting theOriginalData to my theData and did the changes to that and then set my theData back to theOriginalData at the end. It seemed to work better that way. I tried both ways.

But using soucrefiles:“” and then trying to set soucrefiles in table 2 causes the all the sets in table 1 to also have added that record. You will see if you try it out.

Here is the project if you, or anyone else, wants to give it a try. It is just two tables that are bound. I commented out the “good” code if you want to see how it can run perfectly you can reconstitute the good code. As is - I set it up for allowing an empty table 2 which is what I am struggling with here.

http://rdutoit.home.comcast.net/~rdutoit/pub/two_tables_test.zip

Thanks, Rob

OK, you need to use sourcefiles:{} in each instance.

And you can also skip using theOriginalData if you follow up changes to theData like this:

		if bTableView's numberOfRows() = 0 then
			set sourcefiles of item actualSelectedRow of my theData to {{filename:"somefile", filePath:"pathtofile"}}
			set my theData to my theData -- registers change

In fact, this lets you do away with that whole test, replaced by:

		set end of sourcefiles of item actualSelectedRow of my theData to {filename:"somefile", filePath:"pathtofile"}
		set my theData to my theData

You can also set the selection in table 1 like this:

		set x to current application's class "NSIndexSet"'s indexSetWithIndex_(setSelection)
		aTableView's selectRowIndexes_byExtendingSelection_(x, false)

Hmmm, doing it that way you also need to trap for for when there’s no selection in table 1 and someone hits Add for table 2. So maybe:

		if actualSelectedRow > 0 then
			set end of sourcefiles of item actualSelectedRow of my theData to {filename:"somefile", filePath:"pathtofile"}
			set my theData to my theData
			set x to current application's class "NSIndexSet"'s |indexSetWithIndex_|(setSelection)
			aTableView's selectRowIndexes_byExtendingSelection_(x, false)
		end if

And you need to capitalize the “P” in filePath in your binding of the second column of the second table.

You were so close…

I tried that and it didn’t work…

Brilliant. I knew there must be a way to place that more simply.

I actually tried “set my theData to my theData” to see if it would give everything a kick but no luck. But other things were off.

I looked up NSIndexSet and got scared - I had been doing pretty well with OBJ-C and Cocoa stuff till then.

I never would have seen that! One of the major mistakes I keep making is with punctuation. But you know- my applescript could get pretty sloppy with naming conventions and ASOC is forcing me to clean up my act.

A good lesson here; you can try things that will work along the way but you need to know why they are right to write code well.

I got burned out with bindings and lost my way, and missed some crucial things. You have been a great help Shane - thanks.

And this does show the really good possibilities with ASOC going forward- so much less code! I will put together the Two tables as an example project.

Rob

Hi Shane,

I just found one more thing.

When you delete the last row in table 2, you can no longer add a new row. This one seems trickier since the remove call is built and we have no control over it. I’ll ponder this… perhaps a manual remove handler instead.

Rob