Re: Sorting an NSTableView bound to an NSArrayController.
Re: Sorting an NSTableView bound to an NSArrayController.
- Subject: Re: Sorting an NSTableView bound to an NSArrayController.
- From: Keary Suska <email@hidden>
- Date: Thu, 28 Jun 2012 16:12:19 -0600
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"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden