On Mar 3, 2010, at 3:39 PM, Eli Bach wrote:

> I have a core-data app, with a view that shows a managed object, along with 
> an NSCollectionView displaying related managed objects in 1 column, all 
> hooked together primarily with bindings.
> 
> This works fine (selecting different objects automatically updates the 
> collectionview and I can add/edit/delete related objects).
> 
> However, I would like to present the related records in the same order each 
> time, as well as have new objects appear in a predicable location (namely, at 
> one 'end' of the collectionview.  I have an 'order' attribute, with 
> NSInteger16 values, that I want to sort on.
> 
> Just adding a NSSortDescriptor to the NSArrayController that the 
> NSCollectionView gets it's managed objects from only partially works:
> 
> 1) it ONLY does the sort if I have a button that changes the sort AFTER the 
> array is fully populated.  Setting the sortDescriptor on the 
> NSArrayController when the view loads, or even as the root managed object is 
> changed, doesn't result in the collectionview being sorted
> 
> 2) adding or deleting a related record [removing/adding objects for the 
> collectionview] randomizes the order of the objects presented in the 
> collectionview [either by calling add: on the NSArrayController or directly 
> creating a new NSManagedObject for the relation].  The object is 
> presented/removed, but the order is always rearranged randomly.
> 
> I've even tried (programmatically):
> -add/delete record
> -clear sort descriptor
> -set sort descriptor
> 
> and it does do the sort.  It only applies the sort if I hit a button that 
> changes the sortDescriptor on the NSArrayController AFTER the collectionview 
> gets fully populated.
> 
> Any ideas on the best way to keep the nsarraycontroller and/or the 
> nscollectionview sorted all the time?

To follow up on this, I've found what seems to be a stupid 'solution' to this, 
namely subclass NSArrayController and override one method with this:

- (NSArray *)arrangeObjects:(NSArray *)objects
{
        NSArray *       returnValue = objects;
        NSArray *       sortDescriptors = [self sortDescriptors];
        
        if ((sortDescriptors != nil) && ([sortDescriptors count] > 0))
        {
                returnValue = [objects 
sortedArrayUsingDescriptors:sortDescriptors];
        }
        else
        {
                returnValue = [super arrangeObjects:objects];
        }
        
        return returnValue;
}

This results in the collectionview being sorted.

But I expected the arraycontroller to do this automatically just by setting 
sortdescriptor, and perhaps setAutomaticallyRearrangesObjects:YES, on the 
arraycontroller, but it doesn't seem to reliably apply the sort by itself.

Am I misunderstanding NSArrayController, or binding the collectionview to the 
wrong part [it's bound to arrangedObjects], or a bug in NSArrayController?

Eli

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to