Professional Web Applications Themes

Sorting NSTableView contents - Mac Programming

I'd like to be able to sort the contents of a given column in a table by title or number or whatever in way similar to that in iTunes - iow click on the column header and the little triangle points up or down and the contents of the column is sorted accordingly. But I can't find anything in IB that'll give me the triangle in the column header, nor can I see anything in NSTableView that'll implement what I have in mind. I suppose I could use the sorting methods in NSArray or NSMutableArray to do this, but this ...

  1. #1

    Default Sorting NSTableView contents

    I'd like to be able to sort the contents of a given column in a table
    by title or number or whatever in way similar to that in iTunes - iow
    click on the column header and the little triangle points up or down
    and the contents of the column is sorted accordingly. But I can't find
    anything in IB that'll give me the triangle in the column header, nor
    can I see anything in NSTableView that'll implement what I have in
    mind. I suppose I could use the sorting methods in NSArray or
    NSMutableArray to do this, but this seems like something that should
    be doable either via IB or NSTableView.

    Suggestions?

    --
    C Lund, www.notam02.no/~clund
    C Guest

  2. #2

    Default Re: Sorting NSTableView contents

    In article <chello.com>,
    C Lund <no> wrote:
     

    If you're not in 10.3, you have to do the sorting yourself. I have some
    code that does exactly what you need, though. :)

    First, override tableView:shouldSelectTableColumn: in your delegate.
    Columns are selected when the user clicks in the header, so that's how I
    get the clicks. There's probably another, maybe better way to do this,
    but that's how I do it. Then what I do is get the identifier of the
    table column, and that tells me how things should be sorted. If the
    identifier is the same as the previous identifier, then that's my signal
    to flip from ascending to descending order. Then do this:

    [tableView setIndicatorImage:nil inTableColumn:oldColumn]
    [tableView setIndicatorImage:[NSImage
    imageNamed:"NSAscendingSortIndicator"] inTableColumn:tableColumn]

    (Substitute Descending when appropriate.) This sets the little triangle
    thing, and removes it from the old column.

    That takes care of the interface question. Then you have to actually
    sort your data. I have an update method that re-does all of my data,
    which includes sorting, and then tells the table view to reload its
    data. My list is a single array of objects, where each columns uses a
    different selector to get information from those objects. My sort
    function uses NSMutableArray's sortUsingFunction:context: to sort that
    array using the same selectors that are used to get the table columns.
    (I hope that's all suitably unclear....)

    From what I understand, this is all very easy to accomplish using
    NSArrayController and NSSortDescriptor in 10.3, but I have no idea how
    they work. As I recall, you're using some ancient version of OS X, in
    which case you can't use them anyway.
    Michael Guest

  3. #3

    Default Re: Sorting NSTableView contents

    In article <mail-9AE222.22063314112003localhost>,
    Michael Ash <com> wrote:
     
     

    Would the above need to be called in 10.3 as well?
     

    That's pretty much what I thought I might have to do - sort the array
    itself rather than simply have it sorted in the display. I was kinda
    hoping there would be a simple sorting method or two there though - or
    even better, a NIB thing you just drag on the column header in the
    NSTextView. Maybe in 10.4...? ;)
     

    10.1.5.. ;)

    I plan on upgrading to 10.3 at the end of the month or so, so I think
    I'll (once again) just leave the issue until then. BTW: I took a look
    at the online doentation (I usually use Cocoa Browser for
    reference) and I found (void)setSortDescriptors:(NSArray *)array in
    the NSTextView docs. That's what you meant, yes? (plus the
    NSSortDescriptor class itself)

    --
    C Lund, www.notam02.no/~clund
    C Guest

  4. #4

    Default Re: Sorting NSTableView contents

    In article <chello.com>,
    C Lund <no> wrote:
     
    >
    > Would the above need to be called in 10.3 as well?[/ref]

    I think it would depend on how you're doing the sorting. If you're doing
    old-style non-NSController-based stuff, yes. Otherwise, the Controller
    layer should handle it for you. But I haven't looked into NSController
    too much so I don't know for sure.
     

    It's really not that hard to do. Implementing functionality where the
    user can control-click in the table's headers to get a menu with a list
    of columns he can turn on or off, and having the program remember which
    columns are active or not, where they are, and what size they are was
    much more complicated. :)
     

    That sounds vaguely correct. The whole controller layer is still a
    mystery to me, mostly because I haven't put any effort into
    understanding it yet.
    Michael Guest

  5. #5

    Default Re: Sorting NSTableView contents

    For the triangle, I don't think there is a 'standard' one, what you
    need os

    NSTableView->get the NSTableColumn->get headerCell()-> use setImage on
    this object.

    It's taking the scenic route, but it should do the job.

    For the sorting, I don't think it will do it for you, I think you need
    to use catch the user click event on the header, and do it yourself in
    the DataSource.

    Cheers

    Moray





    chello.com>... 
    M Guest

  6. #6

    Default Re: Sorting NSTableView contents

    In article <mail-07CDC0.23064714112003localhost>,
    Michael Ash <com> wrote:
     
    >
    > That sounds vaguely correct. The whole controller layer is still a
    > mystery to me, mostly because I haven't put any effort into
    > understanding it yet.[/ref]

    The brief look I had at the docs gave me the impression I wouldn't
    have to do much more than set up a sortDescriptor and then feed that
    to setSortDescriptors. But I'll find out in a few weeks. B)

    --
    C Lund, www.notam02.no/~clund
    C Guest

Similar Threads

  1. Sorting the contents of a list backwards
    By minerale webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 4
    Last Post: December 1st, 11:15 PM
  2. Navigating in NSTableView
    By Loic in forum Mac Programming
    Replies: 3
    Last Post: October 23rd, 12:23 AM
  3. help w/ multiple columns in NSTableView
    By Willian Irving Zumwalt in forum Mac Programming
    Replies: 2
    Last Post: September 23rd, 03:05 AM
  4. sorting on hash contents
    By Mark Probert in forum Ruby
    Replies: 3
    Last Post: August 28th, 04:51 PM
  5. [Cocoa] Bug with NSTableView?
    By Mark Bee in forum Mac Programming
    Replies: 1
    Last Post: July 7th, 07:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139