View Full Version : NSTableView, sorting by ascending/descending and showing priority
Jul 5, 2009, 07:30 PM
on an NSTableView is it possible to..
- click on the column header cell
- have the contents sorted by ascending or descending order
- show that in the column is sorted by ascending or descending order by having a little triangle
- show the sort priority of the column (this would be shown as a number on the header cell and my sorting algorithm will sort based on the numbers in the header cell)
- and also allow the user to select the priority by right clicking and choosing from a context click menu?
Jul 5, 2009, 08:07 PM
nevermind, i didn't see this method in the NSTableHeaderCell
it looks promising
Jul 5, 2009, 08:13 PM
All that sorting stuff is done for you automatically. If you click on a table column in IB you can give it a sort key and selector. Then you can implement tableView:sortDescriptorsDidChange: which gets called whenever a user clicks on a column. Then you just sort your array via sortUsingDescriptors: and reload the table.
The other priority thing would have to be custom.
Jul 5, 2009, 11:10 PM
requires me to know the frame that the cell is being drawn in...
how am i meant to get that frame? none of the NSTableHeaderCell, NSTextFieldCell, NSActionCell or NSCell have any method or returning an NSRect without me knowing what it is to begin with...
Jul 5, 2009, 11:14 PM
Not sure I fully understand the question. You'd have to subclass NSTableHeaderCell and override that method, and then do all your drawing inside cellFrame.
Jul 5, 2009, 11:27 PM
surely there's a method that you don't have to override to get the little triangle drawn in the header cell.... like the one you see in Mail
that has the method i thought would draw the triangle...
the problem is... i don't know what cellFrame (in the first method) and theRect (in the second method) are meant to be, or where you get that info from
Jul 5, 2009, 11:32 PM
That triangle is automatically drawn for you if you follow the steps I posted above. In the "old" days of Cocoa you had to do it yourself using private images in the framework, but now Apple does it all for you. You just setup the sort descriptor stuff and sort your array, Cocoa takes care of the rest :)
Edit: ah I got confused by the term priority. I usually refer to that as sort indicator, aka the triangle.
Jul 6, 2009, 12:02 AM
the method i'm after resides in the NSTableView
- (void)setIndicatorImage:(NSImage *)anImage inTableColumn:(NSTableColumn *)aTableColumn
edit: i will find a way to indicate the sort priority should be easy enough i hope... i'll just use a string in front of the header cell's string value
Jul 16, 2009, 11:31 PM
well, the problem was solved, but i didn't use any of the sorting functions provided by objective c
why? because that sorting thing only seems to sort by one property.
if i have two objects that have the same NSString that it's being sorted on and i want it to resolve this by sorting by some other property of those objects it didn't seem doable with the keypath thing, because you can only have one
i'm prepared to admit that what i just said is wrong, because something as cool as keypaths would surely be able to do something so simple and common... i would hope anyway...
anywho, i made my own sort, based on mergesort, it works perfectly.
it would be nice to know for future reference how to do the sorting with all the cool features of objective c... when two things have the same property that's being compared to start with and then resolving it to the next thing...
because even though merge sort is really fast ( O(nlogn) on average and worst case), i'm fairly certain NSMutableArrays are pretty slow at copying and inserting and removing. didn't have enough data to see any noticeable slow downs though