Table sort image question
-
Namaste!
I have the following call:
- (void)tableView:(NSTableView *)tv didClickTableColumn:(NSTableColumn
*)tableColumn
{
NSImage *sortImage = [tv indicatorImageInTableColumn:tableColumn];
...
}
The tableColumn in question has an up-triangle in the right side of the
column header.
Can anyone tell me why sortImage comes back with a 0x0 value?
Essentially what I’m trying to do is determine the sort of the column. One
way I've found is to pull out the indicator image. I plan on comparing that
to a named image for one of the indicator images. If there is a better way
to do that, I'd love to know as I haven't dredged anything up on that yet.
Peace, Love, and Light,
Jon C. Munson II
"And Jesus looking upon them saith, With men it is impossible, but not with
God: for with God all things are possible." [Mark 10:27; KJV]
I sign "Peace, Love, and Light" for at least two reasons. First, it is my
truest desire for this planet and her people to live in Peace, with Love,
and in the Light of God. Second, to be an Ascended Master, one must Be. As
I wish to Be an Ascended Master (someday if not sooner), I must also Be -
thus I choose to Be Peace, Love, and Light as much as I can for everyone and
am therefore reflecting those thoughts to you. -
On 6 Jan 2009, at 6:20 am, Jon C. Munson II wrote:
> Essentially what I’m trying to do is determine the sort of the
> column. One
> way I've found is to pull out the indicator image. I plan on
> comparing that
> to a named image for one of the indicator images. If there is a
> better way
> to do that, I'd love to know as I haven't dredged anything up on
> that yet.
Is there a better way? Let me put it this way: could there possibly be
a worse way?
This is nuts.
Look into sort descriptors. Presumably you are trying to determine
which direction the column is sorted in so that you can sort some data
accordingly? (Otherwise, what are you trying to do?). The table view
maintains sort descriptors which not only contain the direction but
the sort key and selector to use for sorting, and these descriptors
can be simply passed to a mutable array to sort the array accordingly.
The table then redisplays the contents. The table's data source can be
informed of a change to the sort descriptors by the user (like when
they click a column header) and the data can be resorted.
--Graham -
> Essentially what I'm trying to do is determine the sort of the
> column. One
> way I've found is to pull out the indicator image. I plan on
> comparing that
> to a named image for one of the indicator images. If there is a
> better way
> to do that, I'd love to know as I haven't dredged anything up on
> that yet.
Is there a better way? Let me put it this way: could there possibly be
a worse way?
[Jon C. Munson II] Yes, there is a better way. I found it later (as you
point out below). And there most certainly could be a worse way.
This is nuts.
[Jon C. Munson II] I thought so as well, hence my query.
Look into sort descriptors. Presumably you are trying to determine
which direction the column is sorted in so that you can sort some data
accordingly? (Otherwise, what are you trying to do?). The table view
maintains sort descriptors which not only contain the direction but
the sort key and selector to use for sorting, and these descriptors
can be simply passed to a mutable array to sort the array accordingly.
The table then redisplays the contents. The table's data source can be
informed of a change to the sort descriptors by the user (like when
they click a column header) and the data can be resorted.
[Jon C. Munson II] Yes to the first question (I'm trying to sync up a
coverflow with a tableview, much like Finder). I looked into the sort
descriptors and that is helping. However, I have a couple more questions:
1. When is didClickTableColumn actually called? Is it after the table does
its stuff, including setting the sort descriptors, or just prior to that?
2. In attempting to answer #1, I ran into an issue with using the
-ascending message. The doc states its returns a bool, however, I could not
use the message. Any ideas there?
[Jon C. Munson II] -
On 7 Jan 2009, at 12:18 am, Jon C. Munson II wrote:
> 1. When is didClickTableColumn actually called? Is it after the
> table does
> its stuff, including setting the sort descriptors, or just prior to
> that?
This is not the method you want. You want the NSTableDataSource method:
- (void) tableView:(NSTableView*) aTableView sortDescriptorsDidChange:
(NSArray*) oldDescriptors
In this method, you can retrieve the *current* sortDescriptors from
the tableview, and sort a mutable array using:
- (void) sortUsingDescriptors:(NSArray*) sortDescriptors
Note that the sort descriptors passed in the first method are the old
ones, not the current ones, so you have to go back to the table to get
the new ones. Having sorted the array, you need to reload the table to
display the outcome. That's all - there's nothing else you need to do
to support sorting from table columns except setting up the sort key
and selector in IB.
> 2. In attempting to answer #1, I ran into an issue with using the
> -ascending message. The doc states its returns a bool, however, I
> could not
> use the message. Any ideas there?
I don't see any reason to actually try and access the sort descriptor
itself - just use it as a black box. Note that sort descriptors come
in an array, so there could be several, one for each sortable column.
I can't see immediately in the docs what order they come in, so you
can't necessarily tell which sort descriptor represents a particular
column (though my *guess* is that the first one is the selected column
header).
hth,
Graham -
On 7 Jan 2009, at 12:18 am, Jon C. Munson II wrote:
> 1. When is didClickTableColumn actually called? Is it after the
> table does
> its stuff, including setting the sort descriptors, or just prior to
> that?
This is not the method you want. You want the NSTableDataSource method:
- (void) tableView:(NSTableView*) aTableView sortDescriptorsDidChange:
(NSArray*) oldDescriptors
In this method, you can retrieve the *current* sortDescriptors from
the tableview, and sort a mutable array using:
- (void) sortUsingDescriptors:(NSArray*) sortDescriptors
[Jon C. Munson II] Thank you for this, I'm looking into it now.
Note that the sort descriptors passed in the first method are the old
ones, not the current ones, so you have to go back to the table to get
the new ones. Having sorted the array, you need to reload the table to
display the outcome. That's all - there's nothing else you need to do
to support sorting from table columns except setting up the sort key
and selector in IB.
[Jon C. Munson II] OK. Thank you for that. I decided to stick with the
delegate method of didClickTableColumn as it functionally provides the same
thing (at this time) without having to create yet another class,
instantation, etc. I will keep that in mind for future use though, so I
appreciate the pointer to it.
> 2. In attempting to answer #1, I ran into an issue with using the
> -ascending message. The doc states its returns a bool, however, I
> could not
> use the message. Any ideas there?
I don't see any reason to actually try and access the sort descriptor
itself - just use it as a black box. Note that sort descriptors come
in an array, so there could be several, one for each sortable column.
I can't see immediately in the docs what order they come in, so you
can't necessarily tell which sort descriptor represents a particular
column (though my *guess* is that the first one is the selected column
header).
[Jon C. Munson II] I found in subsequent testing that didClickTableColumn
appears to get called after the table sets its sort descriptors, etc. So,
as you pointed out, I didn't need to do anything really with the sort
descriptor other than "pass it along." Now, I've got to unhook the sortKeys
in my hack of covertflow and use the sort descriptor as passed. Adapt and
overcome....
Thank you for your help so far. -
OK, I looked into the method:
- (void) tableView:(NSTableView*) aTableView sortDescriptorsDidChange:
(NSArray*) oldDescriptors
And decided to give it a shot after much play with the prior method. In
that prior method I found that the sortDescriptors and Prototype were not
getting changed as expected, thus my use of those items wasn't valid.
Certain values in the descriptors weren't changing as expected.
So, I created a subclass of NSTableView to implement the method above.
I found that sortDescriptorsDidChange is not getting called.
I am not able to find any further reference to implementation of that method
to make sure I implemented it correctly.
My subclass isn't complicated, essentially containing a pointer to the view
which needs to stay in sync with the table (so I can call a method to update
the sortDescriptors for the underlying data), and the method above.
Did I miss something? Based on what I see, I think the sortDescriptors
aren't even being used when the column sort happens...or is that wrong
thinking?
Thanks in advance!
Peace, Love, and Light,
/s/ Jon C. Munson II
-----Original Message-----
From: Jon C. Munson II [mailto:<jmunson...>]
Sent: Tuesday, January 06, 2009 10:09 AM
To: 'Graham Cox'
Cc: 'cocoa dev'
Subject: RE: Table sort image question
On 7 Jan 2009, at 12:18 am, Jon C. Munson II wrote:
> 1. When is didClickTableColumn actually called? Is it after the
> table does
> its stuff, including setting the sort descriptors, or just prior to
> that?
This is not the method you want. You want the NSTableDataSource method:
- (void) tableView:(NSTableView*) aTableView sortDescriptorsDidChange:
(NSArray*) oldDescriptors
In this method, you can retrieve the *current* sortDescriptors from
the tableview, and sort a mutable array using:
- (void) sortUsingDescriptors:(NSArray*) sortDescriptors
[Jon C. Munson II] Thank you for this, I'm looking into it now.
Note that the sort descriptors passed in the first method are the old
ones, not the current ones, so you have to go back to the table to get
the new ones. Having sorted the array, you need to reload the table to
display the outcome. That's all - there's nothing else you need to do
to support sorting from table columns except setting up the sort key
and selector in IB.
[Jon C. Munson II] OK. Thank you for that. I decided to stick with the
delegate method of didClickTableColumn as it functionally provides the same
thing (at this time) without having to create yet another class,
instantation, etc. I will keep that in mind for future use though, so I
appreciate the pointer to it.
> 2. In attempting to answer #1, I ran into an issue with using the
> -ascending message. The doc states its returns a bool, however, I
> could not
> use the message. Any ideas there?
I don't see any reason to actually try and access the sort descriptor
itself - just use it as a black box. Note that sort descriptors come
in an array, so there could be several, one for each sortable column.
I can't see immediately in the docs what order they come in, so you
can't necessarily tell which sort descriptor represents a particular
column (though my *guess* is that the first one is the selected column
header).
[Jon C. Munson II] I found in subsequent testing that didClickTableColumn
appears to get called after the table sets its sort descriptors, etc. So,
as you pointed out, I didn't need to do anything really with the sort
descriptor other than "pass it along." Now, I've got to unhook the sortKeys
in my hack of covertflow and use the sort descriptor as passed. Adapt and
overcome....
Thank you for your help so far. -
On Jan 6, 2009, at 2:18 PM, Jon C. Munson II wrote:
> OK, I looked into the method:
>
> - (void) tableView:(NSTableView*) aTableView
> sortDescriptorsDidChange:
> (NSArray*) oldDescriptors
>
> And decided to give it a shot after much play with the prior
> method. In
> that prior method I found that the sortDescriptors and Prototype
> were not
> getting changed as expected, thus my use of those items wasn't valid.
> Certain values in the descriptors weren't changing as expected.
>
> So, I created a subclass of NSTableView to implement the method above.
>
> I found that sortDescriptorsDidChange is not getting called.
You are supposed to implement the above method in the table view's
*data source*.
<http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals
/CommunicatingWithObjects/chapter_6_section_4.html>
The full name of the method is tableView:sortDescriptorsDidChange:.
It takes two arguments, the first being the table view whose sort
descriptors changed.
--Andy
>
>
> I am not able to find any further reference to implementation of
> that method
> to make sure I implemented it correctly.
>
> My subclass isn't complicated, essentially containing a pointer to
> the view
> which needs to stay in sync with the table (so I can call a method
> to update
> the sortDescriptors for the underlying data), and the method above.
>
> Did I miss something? Based on what I see, I think the
> sortDescriptors
> aren't even being used when the column sort happens...or is that wrong
> thinking?
>
> Thanks in advance!
>
> Peace, Love, and Light,
>
> /s/ Jon C. Munson II
>
> -----Original Message-----
> From: Jon C. Munson II [mailto:<jmunson...>]
> Sent: Tuesday, January 06, 2009 10:09 AM
> To: 'Graham Cox'
> Cc: 'cocoa dev'
> Subject: RE: Table sort image question
>
>
> On 7 Jan 2009, at 12:18 am, Jon C. Munson II wrote:
>
>> 1. When is didClickTableColumn actually called? Is it after the
>> table does
>> its stuff, including setting the sort descriptors, or just prior to
>> that?
>
>
> This is not the method you want. You want the NSTableDataSource
> method:
>
> - (void) tableView:(NSTableView*) aTableView
> sortDescriptorsDidChange:
> (NSArray*) oldDescriptors
>
> In this method, you can retrieve the *current* sortDescriptors from
> the tableview, and sort a mutable array using:
>
> - (void) sortUsingDescriptors:(NSArray*) sortDescriptors
> [Jon C. Munson II] Thank you for this, I'm looking into it now.
>
> Note that the sort descriptors passed in the first method are the old
> ones, not the current ones, so you have to go back to the table to get
> the new ones. Having sorted the array, you need to reload the table to
> display the outcome. That's all - there's nothing else you need to do
> to support sorting from table columns except setting up the sort key
> and selector in IB.
>
> [Jon C. Munson II] OK. Thank you for that. I decided to stick with
> the
> delegate method of didClickTableColumn as it functionally provides
> the same
> thing (at this time) without having to create yet another class,
> instantation, etc. I will keep that in mind for future use though,
> so I
> appreciate the pointer to it.
>
>> 2. In attempting to answer #1, I ran into an issue with using the
>> -ascending message. The doc states its returns a bool, however, I
>> could not
>> use the message. Any ideas there?
>
> I don't see any reason to actually try and access the sort descriptor
> itself - just use it as a black box. Note that sort descriptors come
> in an array, so there could be several, one for each sortable column.
> I can't see immediately in the docs what order they come in, so you
> can't necessarily tell which sort descriptor represents a particular
> column (though my *guess* is that the first one is the selected column
> header).
>
>
> [Jon C. Munson II] I found in subsequent testing that
> didClickTableColumn
> appears to get called after the table sets its sort descriptors,
> etc. So,
> as you pointed out, I didn't need to do anything really with the sort
> descriptor other than "pass it along." Now, I've got to unhook the
> sortKeys
> in my hack of covertflow and use the sort descriptor as passed.
> Adapt and
> overcome....
>
> Thank you for your help so far.
-
Thanks for that correction.
I created a subclass of an NSArrayController (which provides the data from a
CoreData entity to the table) and implemented the method in the subclass.
At first I had a little trouble, then, after scanning the ref you sent, I
set the dataSource outlet for the tableView to the NSArrayController (which
I wasn't sure if I needed to do or if it would "break" anything), and it
seems to work just peachy.
One more follow-on question: if one is using an NSArrayController to
provide data to tableView columns, is it wise to set the dataSource outlet
to that controller or does doing so create unnecessary overhead?
Peace, Love, and Light,
/s/ Jon C. Munson II
-----Original Message-----
From: Andy Lee [mailto:<aglee...>]
Sent: Tuesday, January 06, 2009 2:31 PM
To: <jmunson...>
Cc: 'Graham Cox'; 'cocoa dev'
Subject: Re: Table sort image question
On Jan 6, 2009, at 2:18 PM, Jon C. Munson II wrote:
> OK, I looked into the method:
>
> - (void) tableView:(NSTableView*) aTableView
> sortDescriptorsDidChange:
> (NSArray*) oldDescriptors
>
> And decided to give it a shot after much play with the prior
> method. In
> that prior method I found that the sortDescriptors and Prototype
> were not
> getting changed as expected, thus my use of those items wasn't valid.
> Certain values in the descriptors weren't changing as expected.
>
> So, I created a subclass of NSTableView to implement the method above.
>
> I found that sortDescriptorsDidChange is not getting called.
You are supposed to implement the above method in the table view's
*data source*.
<http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals
/CommunicatingWithObjects/chapter_6_section_4.html>
The full name of the method is tableView:sortDescriptorsDidChange:.
It takes two arguments, the first being the table view whose sort
descriptors changed.
--Andy
>
>
> I am not able to find any further reference to implementation of
> that method
> to make sure I implemented it correctly.
>
> My subclass isn't complicated, essentially containing a pointer to
> the view
> which needs to stay in sync with the table (so I can call a method
> to update
> the sortDescriptors for the underlying data), and the method above.
>
> Did I miss something? Based on what I see, I think the
> sortDescriptors
> aren't even being used when the column sort happens...or is that wrong
> thinking?
>
> Thanks in advance!
>
> Peace, Love, and Light,
>
> /s/ Jon C. Munson II
>
> -----Original Message-----
> From: Jon C. Munson II [mailto:<jmunson...>]
> Sent: Tuesday, January 06, 2009 10:09 AM
> To: 'Graham Cox'
> Cc: 'cocoa dev'
> Subject: RE: Table sort image question
>
>
> On 7 Jan 2009, at 12:18 am, Jon C. Munson II wrote:
>
>> 1. When is didClickTableColumn actually called? Is it after the
>> table does
>> its stuff, including setting the sort descriptors, or just prior to
>> that?
>
>
> This is not the method you want. You want the NSTableDataSource
> method:
>
> - (void) tableView:(NSTableView*) aTableView
> sortDescriptorsDidChange:
> (NSArray*) oldDescriptors
>
> In this method, you can retrieve the *current* sortDescriptors from
> the tableview, and sort a mutable array using:
>
> - (void) sortUsingDescriptors:(NSArray*) sortDescriptors
> [Jon C. Munson II] Thank you for this, I'm looking into it now.
>
> Note that the sort descriptors passed in the first method are the old
> ones, not the current ones, so you have to go back to the table to get
> the new ones. Having sorted the array, you need to reload the table to
> display the outcome. That's all - there's nothing else you need to do
> to support sorting from table columns except setting up the sort key
> and selector in IB.
>
> [Jon C. Munson II] OK. Thank you for that. I decided to stick with
> the
> delegate method of didClickTableColumn as it functionally provides
> the same
> thing (at this time) without having to create yet another class,
> instantation, etc. I will keep that in mind for future use though,
> so I
> appreciate the pointer to it.
>
>> 2. In attempting to answer #1, I ran into an issue with using the
>> -ascending message. The doc states its returns a bool, however, I
>> could not
>> use the message. Any ideas there?
>
> I don't see any reason to actually try and access the sort descriptor
> itself - just use it as a black box. Note that sort descriptors come
> in an array, so there could be several, one for each sortable column.
> I can't see immediately in the docs what order they come in, so you
> can't necessarily tell which sort descriptor represents a particular
> column (though my *guess* is that the first one is the selected column
> header).
>
>
> [Jon C. Munson II] I found in subsequent testing that
> didClickTableColumn
> appears to get called after the table sets its sort descriptors,
> etc. So,
> as you pointed out, I didn't need to do anything really with the sort
> descriptor other than "pass it along." Now, I've got to unhook the
> sortKeys
> in my hack of covertflow and use the sort descriptor as passed.
> Adapt and
> overcome....
>
> Thank you for your help so far.



