I had found that list of methods and insertObject:atArrangedObjectIndex: seems like the one. However I couldn’t figure out to implement it. I kept getting errors.
Still trying to figure out the sort. I have it working where I update the data and tell the view to update. Is there a way that the controller could sort the data automatically?
With this option when you update the table row the row doesn’t stays selected (which I need)
tell theArrayController to removeObjectAtArrangedObjectIndex_(myTableView's selectedRow as integer)
tell theArrayController to insertObject_atArrangedObjectIndex_({itemOne:itemOne, itemTwo:itemTwo, itemThree: itemThree}, myTableView's selectedRow as integer)
Trying to find rearrangeObjects in the controller now for sort.
I did find this code which works (but only if a sort column is selected by user)
set sortDesc to myTableView's sortDescriptors()
theData's sortUsingDescriptors_(sortDesc)
myTableView's reloadData()
I’m trying to sort the array by the key “itemOne” every time a new entry is added to the array.
I wonder whether it might be worth rethinking your UI a bit.
The simplest table involves just editing values in place, but sometimes you need them in separate fields. But if you bind the contents of those fields to the array controller with a Controller Key of selection and a suitable Model Key Path, you get the benefit of a separate entry area but without the need for lots of code. You just click on an entry, and the values appear in the fields; edit the fields, and the table updates.
New issue: If I sort the table by clicking the table column header and use the code below the selectedRow index doesn’t match up with the what’s in theData.
set my itemOne to itemOne of item ((myTableView's selectedRow as integer)+1) of theData
Clicking on a column header sets the array controller’s sortDescriptors property. Until you do that, the controller has no sortDescriptors – unless you add them using setSortDescriptors_, passing an array of NSSortDescriptors.
You don’t need the {{ and }} around the value. But yes, you will find a lot of methods that begin with set, and it’s hard getting out of the AS habit of trying to use the set command.