Table View Application

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?

Post some code.

You have to trigger it. Try rearrangeObjects.

Hi Shane,

Thanks for the reply!

I found out how to use insertObject:atArrangedObjectIndex: in your book.

I explored two different ways to update…

With this option when you update the table row the row stays selected


theData's replaceObjectAtIndex_withObject_(myTableView's selectedRow as integer, {itemOne:itemOne, itemTwo:itemTwo, itemThree: itemThree})
myTableView's reloadData()

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 can’t figure that one out. I found a method in your book that sorts lists but it doesn’t work with either theArrayController or theData

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.

Hi Shane,

Thanks again for the reply.

That’s an interesting idea I’ll have to play with that to find out how a new entry would be created.

I’m still trying to figure this sort thing out.

Is there a way to do the following without the user having to click on the column header?

set sortDesc to myTableView's sortDescriptors()
theData's sortUsingDescriptors_(sortDesc)
myTableView's reloadData()

Thanks,
CQ

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

That’s right. The raw data and what’s showing in the table don’t match in order. Get the data directly from the array controller (selectedObjects()).

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.

Shane,

Thanks! I’ll give that a try.

Also, your book is great! It has been so helpful!

CQ

What am I doing wrong?


set theDescriptor to SortDescriptorClass's sortDescriptorWithKey_ascending_("jobDate",true)
log theDescriptor -- (jobDate, ascending, compare:)
theArrayController's sortUsingDescriptors_({theDescriptor})

I get this error:
unrecognized selector sent to instance

I was using sortUsingDescriptors_ instead of setSortDescriptors_

It’s working now. Thanks

Now I’m having an issue trying to update a value of one of the array controller objects by key.


set theArrayController's arrangedObjects()'s objectAtIndex_(0)'s valueForKey_("itemOne") to "A new Value"

I get this error:
Can’t set «class ocid» id «data kptr00000000E098017CFF7F0000» to “A new Value”

I dug through your book and found this and it seems to be working


theArrayController's arrangedObjects()'s objectAtIndex_(0)'s setValue_forKey_({{"A new Value"}},"itemOne")

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.

New issue…

If I connect a date picker object to this function:


on dateChange_(sender)
    log "Change"   
end dateChange_

I Notice it logs “Change” twice. One on mouse down and one on up. How do you detect event type? So the function only executes on one or the other.

actually it appears that it triggers on mouse click and then again if the date was actually changed

any way to listen for a specific event type?

Not without subclassing it. You can always check its before and after value.

Shane,

Thanks I’ll try that.

I’m also trying to implement your helper classes “ObjectWithFords” and I followed these directions:

No matter which helper I try to use I get this error:

I figured it out. I was adding by dragging and not “Add to project”