So, I setup my document based app to detect table selections. This works great. I put in a logging step so I could see what the selection was. Oddly, when I create a new row in the table ” which my code has to select because it wasn’t happening automatically ” THREE selection changes are taking place.
First, the new row is selected, then unselected and then reselected. So, the second selection is my code selecting the row after creating it. If I remove that code, now TWO selections are taking place. First the new row is selected and then UNselected all automatically by the app, I didn’t’ code this.
Any suggestions on what settings I change to make this stop happening. Ideally, I would like the new row to be created and selected.
then it should be unchecked. This tells your array controller to select any and all objects that are inserted, but I usually turn this off because it usually slows down the app, and I have not found it usefull.
-- Setup notification for table selection
set theNotificationCenter to current application's NSNotificationCenter's defaultCenter()
tell theNotificationCenter to addObserver_selector_name_object_(me, "uiTableRowSelected:", "NSTableViewSelectionDidChangeNotification", missing value)
telling this subroutine to log the selected row whenever a change occurs:
on uiTableRowSelected_(sender)
tell refUITable to set numRow to selectedRow()
log "selected row " & numRow
end
Clicking the + button, runs this code:
on uiTableAddNewRow_(sender)
set recData to {{field:"value", field:"value", etc}}
tell refUITableArrayController
addObjects_(recData)
end tell
log "DONE with insertion"
....
After the + button is clicked once, this is in the log:
selected row 0
selected row -1
DONE with insertion
And… nothing is selected in the table!
So, it’s creating it selected and then unselecting it. It seems to completely ignore the checkbox for select inserted items and the same coded setting for the array. No clue what else to do besides select the new row manually (with code) and then ignore the first two selections and only take action on the third, but that seems kind of funky.
OK, I see a huge problem right there. You are using frameworks for the iOS (aka iPhone OS) development, not the Mac OS X frameworks.
Anything that begins with UI in capital is for iPhone apps development, and AppleScriptObjC is NOT supported on iPhones (sadly!).
When it begins with NS (for NextStep, Mac OS X’s ancestor) then you’re in Mac land.
You can create a separate applescript file and create an NSObject cube in your NIB with it’s name as a class, set it as the delegate for your Table View, and then use NSTableViewDelegate’stableViewSelectionDidChange: or tableViewSelectionIsChanging:.
selected row -1 is normal. -1 means no selection. You will always get this when there is no selection after a selection change notification is sent to the table’s delegate.
FYI, row 0 means first row, because most index in Obj-C start with 0, not 1, unlike AppleScript. Probably figured it out by yourself already…
I’ll create a very simple app for you tomorrow that does all that, and share you a dropbox link so you can download and see for yourself. You can learn the same way I did with such examples. If you want me to, of course.
ASOC is very easy to use, once you’ve mastered the idea of objects and instance and class methods. Then Obj-C is much more easier to deal with after that.
FYI the subroutines that start with “ui” are my own subroutines that stand for “user interface”…
Did I happen to name them something “official”? Could that be part of the problem? I don’t know.
Anyway, I hesitate to move the table controlling code to another script file as that will open up a whole other can of worms and anti productive list of new questions for me to deal with. I think for now, unless there is a more obvious answer, I’ll just code around the extraneous selections and ignore them.
For anyone who may need this, my solution was this (hastily extracted so it may contain typos):
global gLabelTableInsertionIgnore -- to work around multiple selections when creating new table rows
on windowControllerDidLoadNib_(aController)
-- Setup notification for table selection
set theNotificationCenter to current application's NSNotificationCenter's defaultCenter()
tell theNotificationCenter to addObserver_selector_name_object_(me, "LabelRowSelected:", "NSTableViewSelectionDidChangeNotification", missing value)
end
on LabelRowSelected_(sender)
if gLabelTableInsertionIgnore = false then
-- Do stuff here
end
end
-- Anywhere I'm inserting a row, or reselecting a row, I do this:
set gLabelTableInsertionIgnore to true
tell refUITableArrayController to addObjects_(recDataToInsert)
set gLabelTableInsertionIgnore to false
it’s a clunky workaround but unless someone knows how to get the table to stop selecting multiple times on it’s own as described above, it works and I’m done.
Posting the entire code or at least the relevant code with property designation is helpful to follow, otherwise, there will a lot off these replies that can be confusing.
I’d love to see an example of insertObject_atArrangedObjectIndex_
I’m noticing as the user selects other elements, such as making a selection in a combo box, a table selection is being registered. That seems odd to me. Again, I can filter it out as I don’t’ know how much time I can spend on this issue but ideally I’d be able to figure out what exactly is going on.
Did you search these forums? Garanteed this has been covered before in here. Tables in general and all the associated problems have been fairly covered over the last few years…
Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)
You can’t search with _ in the method names. This is a compromise made by Apple because : would not work. replace the _ with : and you’ll find them. Or search for NSArrayController, since these are methods that belong to that class.