sorting an NSTableView

  • Hi.  I'm a Cocoa newbie, and I'm trying to figure out how to enable
    sorting columns of my table, but the NSTableView guide doesn't say
    anything about it.  I see that NSTableView has a setSortDescriptors
    method, NSTableColumn has a setSortDescriptorPrototype method, and
    that IB shows a "sort key" attribute for a table column, but I'm not
    clear on how to use them.  Any hints or examples?
  • An Example:

    mTable = member instance for your TableView

    mData = member array which provides the data-objects.

    Put the following code at a place where your table and data-source array is
    initialized. Like in awakeFromNib: method.

    Here I'm sorting on key "name", Change it to whatever key you have in your
    data-object on which you want to do the sorting. You can do the same thing
    from IB as well, then you don't have to write the following code.

    [mTable setSortDescriptors:[NSArray arrayWithObject:

    [[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES

    selector:@selector(caseInsensitiveCompare:) ]autorelease]]];

    [mData sortUsingDescriptors:[mTable sortDescriptors]];

    Now implement the following delegate for NSTableView:

    - (void)tableView: (NSTableView *)aTableView

    sortDescriptorsDidChange: (NSArray *)oldDescriptors

    {

    [mData sortUsingDescriptors: [aTableView sortDescriptors]];

    [mTable reloadData];

    }

    - Vijay

    On Fri, May 30, 2008 at 12:18 PM, James W. Walker <osx...> wrote:

    > Hi.  I'm a Cocoa newbie, and I'm trying to figure out how to enable sorting
    > columns of my table, but the NSTableView guide doesn't say anything about
    > it.  I see that NSTableView has a setSortDescriptors method, NSTableColumn
    > has a setSortDescriptorPrototype method, and that IB shows a "sort key"
    > attribute for a table column, but I'm not clear on how to use them.  Any
    > hints or examples?
    >
  • On May 30, 2008, at 12:48 AM, James W. Walker wrote:

    > Hi.  I'm a Cocoa newbie, and I'm trying to figure out how to enable
    > sorting columns of my table, but the NSTableView guide doesn't say
    > anything about it.  I see that NSTableView has a setSortDescriptors
    > method, NSTableColumn has a setSortDescriptorPrototype method, and
    > that IB shows a "sort key" attribute for a table column, but I'm not
    > clear on how to use them.  Any hints or examples?

    I'm sure Vijay's solution is more correct than mine, but I implement the

    -(void)tableView:(NSTableView *)tableView didClickTableColumn:
    (NSTableColumn *)tableColumn

    delegate method and highlight the column header myself (reversing the
    sort as necessary and setting the right sort direction indicator).
    Then I sort my data source and call reloadData on the table object.

    I found this MacTech article useful for getting to know NSTableView a
    little better also:

    <http://www.mactech.com/articles/mactech/Vol.18/18.09/TableTechniques/index.
    html
    >

    Ken

    --
    Ken Worley
    Software Engineer, Tiberius, Inc.
  • On May 30, 2008, at 5:04 AM, Vijay Malhan wrote:

    > An Example:
    > mTable = member instance for your TableView
    > mData = member array which provides the data-objects.
    >
    > Put the following code at a place where your table and data-source
    > array is initialized. Like in awakeFromNib: method.
    >
    > Here I'm sorting on key "name", Change it to whatever key you have
    > in your data-object on which you want to do the sorting. You can do
    > the same thing from IB as well, then you don't have to write the
    > following code.
    >
    >
    > [mTable setSortDescriptors:[NSArray arrayWithObject:
    > [[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES
    > selector:@selector(caseInsensitiveCompare:) ]autorelease]]];
    >
    > [mData sortUsingDescriptors:[mTable sortDescriptors]];
    >
    >
    > Now implement the following delegate for NSTableView:
    >
    > - (void)tableView: (NSTableView *)aTableView
    > sortDescriptorsDidChange: (NSArray *)oldDescriptors
    > {
    > [mData sortUsingDescriptors: [aTableView sortDescriptors]];
    > [mTable reloadData];
    > }

    Thanks!

    I missed that delegate method, since it's not listed with NSTableView,
    but with NSTableDataSource.  Somehow it works, though it's a little
    hard to wrap my head around.  The sort key is used to extract a string
    from members of the data array,  and the selector is used to compare
    the strings, right?  And if I hadn't represented my table data as an
    NSMutableArray containing NSDictionary objects, I'd have more work to
    do.

    > On Fri, May 30, 2008 at 12:18 PM, James W. Walker <osx...>
    > wrote:
    > Hi.  I'm a Cocoa newbie, and I'm trying to figure out how to enable
    > sorting columns of my table, but the NSTableView guide doesn't say
    > anything about it.  I see that NSTableView has a setSortDescriptors
    > method, NSTableColumn has a setSortDescriptorPrototype method, and
    > that IB shows a "sort key" attribute for a table column, but I'm not
    > clear on how to use them.  Any hints or examples?
  • On Sun, Jun 1, 2008 at 5:53 AM, James W. Walker <osx...> wrote:

    >
    > On May 30, 2008, at 5:04 AM, Vijay Malhan wrote:
    >
    > An Example:
    >> mTable = member instance for your TableView
    >> mData = member array which provides the data-objects.
    >>
    >> Put the following code at a place where your table and data-source array
    >> is initialized. Like in awakeFromNib: method.
    >>
    >> Here I'm sorting on key "name", Change it to whatever key you have in your
    >> data-object on which you want to do the sorting. You can do the same thing
    >> from IB as well, then you don't have to write the following code.
    >>
    >>
    >> [mTable setSortDescriptors:[NSArray arrayWithObject:
    >> [[[NSSortDescriptor alloc]initWithKey:@"name"
    >> ascending:YES
    >> selector:@selector(caseInsensitiveCompare:) ]autorelease]]];
    >>
    >> [mData sortUsingDescriptors:[mTable sortDescriptors]];
    >>
    >>
    >> Now implement the following delegate for NSTableView:
    >>
    >> - (void)tableView: (NSTableView *)aTableView
    >> sortDescriptorsDidChange: (NSArray *)oldDescriptors
    >> {
    >> [mData sortUsingDescriptors: [aTableView sortDescriptors]];
    >> [mTable reloadData];
    >> }
    >>
    >
    > Thanks!
    >
    > I missed that delegate method, since it's not listed with NSTableView, but
    > with NSTableDataSource.  Somehow it works, though it's a little hard to wrap
    > my head around.  The sort key is used to extract a string from members of
    > the data array,

    Yes. The value on which the sorting is to be done.

    > and the selector is used to compare the strings, right?

    Yes.

    > And if I hadn't represented my table data as an NSMutableArray containing
    > NSDictionary objects, I'd have more work to do.

    It has to be a collection/array of sortable objects(and not necessarily a
    Dictionary - see KVC, where the accessor method for a value acts as a key).

    so let's say if you have your object interface like this:

    @interface myObject: NSObject{
          NSString* mName;
          id mOtherData;
    }

    - (NSString *)name;
    - (void)setName: (NSString *)aName;

    @end

    then you can use "name" as a key. Do some reading on KVC for more details.
    An also, with this, these statements will be equivalent:
    [anObject valueForKey@"name"] == [anObject name]
    and
    [anObject setValue: @"someText" forKey: @"name"] == [anObject setName:
    @"someText" ]

    Thanks,
    - VJ

    >
    > On Fri, May 30, 2008 at 12:18 PM, James W. Walker <osx...>
    >> wrote:
    >> Hi.  I'm a Cocoa newbie, and I'm trying to figure out how to enable
    >> sorting columns of my table, but the NSTableView guide doesn't say anything
    >> about it.  I see that NSTableView has a setSortDescriptors method,
    >> NSTableColumn has a setSortDescriptorPrototype method, and that IB shows a
    >> "sort key" attribute for a table column, but I'm not clear on how to use
    >> them.  Any hints or examples?
    >>

    >
previous month may 2008 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 31  
Go to today