NSTableView, sorting by ascending/descending and showing priority

Discussion in 'Mac Programming' started by Chirone, Jul 5, 2009.

  1. macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    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?
     
  2. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #2
    nevermind, i didn't see this method in the NSTableHeaderCell
    drawSortIndicatorWithFrame:inView:ascending:priority:

    it looks promising
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    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.
     
  4. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #4
    the method:
    drawSortIndicatorWithFrame:inView:ascending: priority:
    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...
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    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.
     
  6. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #6
    o_O

    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

    here:
    http://developer.apple.com/document...ableHeaderCell_Class/Reference/Reference.html

    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
     
  7. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    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.
     
  8. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #8
    nevermind :rolleyes:

    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
     
  9. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #9
    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
     

Share This Page