Sorting an NSTableView bound to an NSArrayController.

  • Hello,

    I'm writing an application that handles results for a running club.

    I have an NSTableView that has columns bound to a CoreData Entity such as Entries.arrangedObjects.position, position is an NSString that contains numbers such as @"1" and @"2" or @"" but the position can also be @"DNF" (Did not finish).

    I have a method in the WindowController that sorts the 'Entry' objects and handles these cases correctly returning an NSArray using:

      [array sortUsingComparator:^(id obj1, id obj2)
          {
          NSComparisonResult r;

          r = ....

          return (r);
          }];

    How can I get my table view to work so that when the user clicks in the Pos. column I can do my custom sort? at present it is doing an ASCII sort so I get 1, 10, 100, then 2, 20, 200 etc...

    I now I need to put a key and sort descriptor in IB but don't know what code I have to add to the Entity ot WindowController to handle it.

    Thanks, regards, Rob.
  • On Jun 28, 2012, at 1:16 PM, Robert Tillyard wrote:

    > Hello, Keary,
    >
    > Thank you for your reply.
    >
    > Would the selector in the model be in the class for the CoreData Entity or os it in the WindowController sub-class that controls the window that the TableView is in?
    >
    > Would you know what the method for the selector would look like? I'm guessing it must return an NSComparisionResult but if the entries in the ListView are Entry classes would it be:
    >
    > - (NSComparisonResult)positionCompare:(Entry *)obj1 somethingElse:(Entry *)obj2;

    I forgot about that piece--yes, you will need to implement an NSEntityDescription subclass for the sort method. If that proves to be problematic, you can fall back to binding/delegate that you can implement in your window controller.

    > On 28 Jun 2012, at 19:38, Keary Suska wrote:
    >
    >> On Jun 28, 2012, at 9:44 AM, Robert Tillyard wrote:
    >>
    >>> Hello,
    >>>
    >>> I'm writing an application that handles results for a running club.
    >>>
    >>> I have an NSTableView that has columns bound to a CoreData Entity such as Entries.arrangedObjects.position, position is an NSString that contains numbers such as @"1" and @"2" or @"" but the position can also be @"DNF" (Did not finish).
    >>>
    >>> I have a method in the WindowController that sorts the 'Entry' objects and handles these cases correctly returning an NSArray using:
    >>>
    >>> [array sortUsingComparator:^(id obj1, id obj2)
    >>> {
    >>> NSComparisonResult r;
    >>>
    >>> r = ....
    >>>
    >>> return (r);
    >>> }];
    >>>
    >>> How can I get my table view to work so that when the user clicks in the Pos. column I can do my custom sort? at present it is doing an ASCII sort so I get 1, 10, 100, then 2, 20, 200 etc...
    >>>
    >>> I now I need to put a key and sort descriptor in IB but don't know what code I have to add to the Entity ot WindowController to handle it.
    >>
    >>
    >> Although there may be better ways, the easiest way is to specify the per-column sort in IB, and use the "selector" method for the position column. You would need to implement a sort method in your model, of course. Alternatively you could use the NSTableView delegate method tableView:sortDescriptorsDidChange:. You could use the latter in conjunction with the sortDescriptors binding in the array controller.

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • Op 28 jun 2012, om 17:44 heeft Robert Tillyard het volgende geschreven:

    >
    > I now I need to put a key and sort descriptor in IB but don't know what code I have to add to the Entity ot WindowController to handle it.

    You can create a category of NSString and implement your own compare method. In IB you set the selector of the table column to this method.

    Willeke
  • Thank you, Willeke,

    That worked brilliantly.

    Regards, Rob.

    On 29 Jun 2012, at 22:15, Willeke wrote:

    >
    > Op 28 jun 2012, om 17:44 heeft Robert Tillyard het volgende geschreven:
    >
    >>
    >> I now I need to put a key and sort descriptor in IB but don't know what code I have to add to the Entity ot WindowController to handle it.
    >
    >
    > You can create a category of NSString and implement your own compare method. In IB you set the selector of the table column to this method.
    >
    > Willeke
    >
previous month june 2012 next month
MTWTFSS
        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  
Go to today