Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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

Moderator emeritus
Jun 15, 2000
7,958
7
You could try using addObject: instead, or calling rearrangeObjects after you insert the object.
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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:

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

Moderator emeritus
Jun 15, 2000
7,958
7
How do you expect it to place your object into a certain index when you have it sorted at the same time?
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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?
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.