PDA

View Full Version : Bizarre Bindings Problem




stadidas
Nov 29, 2007, 02:03 PM
Hi all, I have an interesting one today...

I have a NSMutableArray, which is managed by an NSArrayController. I insert objects into the array using the insertObject:atArrangedObjectIndex: method of NSArrayController. I then display this data in an NSTableView.
This all works just fine, I can insert objects and delete them, and the table view is automatically updated. However, when the table view is sorted on one of the columns (via clicking on a column heading), any object added via the insertObject:atArrangedObjectIndex: method is put at the end of the mutable array. For example if I try to insert a new object at index 0 when the array contain six objects, the new object will be inserted into the array at index 6.
This only happens when the table is sorted, otherwise the behaviour is fine. Somewhere the index specified gets changed to the size of the array.
If anyone can shed any light on this issue I would very much appreciate it as once I've fixed this my app will be useable.



kainjow
Nov 29, 2007, 02:11 PM
You could try using addObject: instead, or calling rearrangeObjects after you insert the object.

stadidas
Nov 29, 2007, 02:26 PM
Hi Kainjow,

Thanks for the quick reply.
Perhaps I should make the problem a bit more clear. I can't use addObject: because I need to insert the object into a specific place in the array. What is strange is that I call this code:


[logEntriesController insertObject:newLog atArrangedObjectIndex:indexToInsertLogEntryAt];


where logEntriesController is the NSArrayController. indexToInsertLogEntryAt is zero. However, once I go into my overridden insertObject:atArrangedObjectIndex: method, the index being passed in is the same as the array count that the array controller controls. This is strange as it doesn't happen when the table isn't sorted.

kainjow
Nov 29, 2007, 02:36 PM
How do you expect it to place your object into a certain index when you have it sorted at the same time?

stadidas
Nov 29, 2007, 02:44 PM
I thought the sorting was just in the view? Is there a way to have the view sort but leave the array contents alone? I do some calculation based on the ordering of objects in the array, so it shouldn't be re-arranged by a view.

kainjow
Nov 29, 2007, 02:47 PM
If you want that kind of control (not sure why, as it's non-standard), don't use bindings. Just use normal NSTableView delegate methods and manage two arrays.

But either way, your users will be confused if they see a sorted table with a non-sorted item at the top.

The view is a graphical representation of your model. It doesn't manipulate your model's data. If the table displays sorted data, your data is sorted.

stadidas
Nov 29, 2007, 03:05 PM
The array stores objects of type "LogEntry". A Log Entry stores, amongst other things, a description string and an a date field. The array should always have the log entry with the oldest date at the start, and the newest date at the end. The user may sort the table by the description String to find something, and then may choose to insert a new log entry. The entry still needs to be added in the correct place based on it's date. The correct location is calculated, and then I pass it to the insertObject:AtArrangedObjectIndex method.
Is there anyway that you can see that would allow this to work whilst still using the bindings mechanism?

kainjow
Nov 29, 2007, 03:42 PM
Do you have the table view sort descriptors setup correctly?

stadidas
Nov 29, 2007, 04:08 PM
I have not put any specific sort key or selector in on Interface Builder, no. Would I need to do this?

kainjow
Nov 29, 2007, 04:10 PM
Yes if you want them to be sorted for you. Read the docs on NSSortDescriptor

stadidas
Nov 29, 2007, 06:38 PM
Never mind, I just realised that I can just pass a new log object and index to the object that holds the array, insert it exactly where I want, and the TableView updates through bindings even when sorted, and all the values are calculated properly. Wish I'd realised sooner, sorry for wasting your time! Thanks anyway though, you've helper me and others out on numerous occasions, so thanks very much for that.