Restoring sort order to NSTableView

  • I have an NSArrayController hooked to an array (of NSDictionary) and bound
    to columns in a NSTableView. It all works fine. I added an auto save name to
    the table so that the column widths and ordering are restored upon launch,
    but can't get it to restore the sort order.

    I can force a sort with:

    NSMutableArray      *descriptors = [NSMutableArray arrayWithCapacity:0];
    NSSortDescriptor    *descriptor;

    descriptor=[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
    selector: @selector(compare:)];

    [[myTableView tableColumnWithIdentifier:@"name"]
    setSortDescriptorPrototype:descriptor];

    [descriptors addObject:descriptor];
    [descriptor release];

    [myTableView setSortDescriptors:descriptors];

    Of course this sorts by a fixed column. I was thinking I could call
    sortDescriptors before quitting and save restore this using an NSData, but
    really this should be automated.

    How do do it?

    Thanks,

    Trygve
  • on 11/18/07 7:39 AM, <cocoa...> purportedly said:

    > I have an NSArrayController hooked to an array (of NSDictionary) and bound
    > to columns in a NSTableView. It all works fine. I added an auto save name to
    > the table so that the column widths and ordering are restored upon launch,
    > but can't get it to restore the sort order.

    You could bind the sort descriptors to an NSUserDefaultsController, which is
    probably what you are after.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • On 11/20/07 12:01 AM, "Keary Suska" <hierophant...> wrote:

    > on 11/18/07 7:39 AM, <cocoa...> purportedly said:
    >
    >> I have an NSArrayController hooked to an array (of NSDictionary) and bound
    >> to columns in a NSTableView. It all works fine. I added an auto save name to
    >> the table so that the column widths and ordering are restored upon launch,
    >> but can't get it to restore the sort order.
    >
    > You could bind the sort descriptors to an NSUserDefaultsController, which is
    > probably what you are after.

    Nope, that causes a crash on launch.

    I also get a crash when trying to do it this way:

    NSArray      *descriptors = [myTableView sortDescriptors];
    CFPreferencesSetAppValue (CFSTR("SortOrder"), descriptors, appID);

    The Array is ok:

    <CFArray 0x3c3ea0 [0xa080c1c0]>{type = immutable, count = 1, values = (
        0 : (name, ascending, caseInsensitiveCompare:)

    Crash in CFRetain.

    Any ideas?
  • Ok,

    It works with:

    NSData *sortOrderDesc = [NSArchiver archivedDataWithRootObject:[myTableView
    sortDescriptors]];

    [prefs setObject:sortOrderDesc forKey:@"sortOrderDesc"];

    Why do I have to do it this way if NSSortDescriptor conforms to NSCoding?

    Trygve

    > On 11/20/07 12:01 AM, "Keary Suska" <hierophant...> wrote:
    >
    >> on 11/18/07 7:39 AM, <cocoa...> purportedly said:
    >>
    >>> I have an NSArrayController hooked to an array (of NSDictionary) and bound
    >>> to columns in a NSTableView. It all works fine. I added an auto save name to
    >>> the table so that the column widths and ordering are restored upon launch,
    >>> but can't get it to restore the sort order.
    >>
    >> You could bind the sort descriptors to an NSUserDefaultsController, which is
    >> probably what you are after.
    >
    > Nope, that causes a crash on launch.
    >
    > I also get a crash when trying to do it this way:
    >
    > NSArray      *descriptors = [myTableView sortDescriptors];
    > CFPreferencesSetAppValue (CFSTR("SortOrder"), descriptors, appID);
    >
    > The Array is ok:
    >
    > <CFArray 0x3c3ea0 [0xa080c1c0]>{type = immutable, count = 1, values = (
    > 0 : (name, ascending, caseInsensitiveCompare:)
    >
    > Crash in CFRetain.
    >
    > Any ideas?
    >
  • On Nov 20, 2007, at 5:13 AM, Trygve Inda wrote:

    > NSData *sortOrderDesc = [NSArchiver archivedDataWithRootObject:
    > [myTableView
    > sortDescriptors]];
    > [prefs setObject:sortOrderDesc forKey:@"sortOrderDesc"];
    > Why do I have to do it this way if NSSortDescriptor conforms to
    > NSCoding?
    >
    Because it's not automatically archived if you just set the value
    directly with setObject:forKey:...

    mmalc
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today