Re: Sorting NSTableView columns using new Panther methods
Re: Sorting NSTableView columns using new Panther methods
- Subject: Re: Sorting NSTableView columns using new Panther methods
- From: Brian Ganninger <email@hidden>
- Date: Sat, 27 Dec 2003 20:12:22 -0600
1. Define the NIB file and use the variable name as the sort key and a
comparison function such as the generic compare: for each table column.
(such that a click to sort would automatically handle the
indicator/header column and call compare: using 2 objects' sortKey
value (compare:([object1 sortKey],[object2 sortKey]))
A sample compare -
- (NSComparisonResult)compare:(NSModelObject*)other {
// We want the data to be sorted by name, so we compare [self
pictureName] to [other name]
NSModelObject *_other = (NSModelObject *)other;
return [pictureName compare:[_other pictureName]];
}
Notes: Sort Keys vary by column, the sort descriptor does not.
2. Define your model objects setters & getters and handle your values
accordingly.
3. In your table delegate implement the following method -
- (void)tableView:(NSTableView *)tableView
sortDescriptorsDidChange:(NSArray *)oldDescs
A quick sample of how I sorted therein:
// sort descriptors
NSArray *sortDescriptors = [NSArray arrayWithObject:[oldDescs
objectAtIndex:0]];
// sort picture array, a global array, using built in methods
NSArray *sortedArray = [pictureArray
sortedArrayUsingDescriptors:sortDescriptors];
// synchronize globally and reload table
pictureArray = [sortedArray copy];
[listView reloadData];
Sort order simply indicates the beginning, default state for a column
when clicked to sort. This value is saved with the table's autosaved
settings and when persistence is implied (load a saved file at the same
time as the table) you get seamless sorting with low implementation
overhead.
HTH,
brian ganninger
On Dec 27, 2003, at 7:35 PM, zeno wrote:
>
*** Essential question:
>
>
Can anyone tell us, step by step, what we need to do to sort an
>
NSTableView column (like in Mail or iTunes) using the new "sort
>
descriptors" methods available in OSX 10.3 and the new "Sort Key",
>
"Sort Selector" and "Sort Order" parameters that we find in the new IB
>
that comes with the Xcode tools ?
>
>
>
>
*** Details and what I already know (for the ones who have more time):
>
>
I've read the last AppKit.html (
>
file:///Developer/Documentation/ReleaseNotes/Cocoa/AppKit.html ) and I
>
understood that with the new IB we can automatically manage the
>
columns sorting at least from a "GUI point of view"; in fact, if we
>
double-click an NSTableView in IB, select a column, then we inspect
>
its Attributes and fill out the "Sort Key" textfield (leave for ex.
>
"compare:" as "Sort Selector" and "Ascending" as "Sort Order"
>
parameters) the sorting UI implementation is practically done: no more
>
need to call the "tableView:didClickTableColumn:" method of the
>
NSTableView's delegate, test the sorting order (asc./desc.) and set
>
the "triangle indicator image" calling the [NSImage imageNamed:
>
@"NSAscendingSortIndicator"] or [NSImage imageNamed:
>
@"NSDescendingSortIndicator"] as we did in Jaguar. Those things are
>
automatically done now.
>
>
Now, what I need to understand, is what code I have to add (I suppose
>
to the table datasource) to get the sorting actually working.
>
>
Do I have to specify a sort descriptor as explained here:
>
http://developer.apple.com/documentation/Cocoa/Conceptual/
>
SortDescriptors/index.html ?
>
Do I have to define a sortDescriptorPrototype as explained in the
>
AppKit.html or is it automatically done from IB ?
>
Do I have to implement the "tableView:sortDescriptorsDidChange:"
>
method of NSTableDataSource ?
>
Do I have to use some of the IB "Bindings" (from the "Show Info"
>
panel) of the NSTableView ?
>
I'm confused. Sorry
>
;-)
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.