Re: Sorting NSTableView using Bindings
Re: Sorting NSTableView using Bindings
- Subject: Re: Sorting NSTableView using Bindings
- From: Ken Thomases <email@hidden>
- Date: Mon, 05 Nov 2012 19:35:43 -0600
On Nov 5, 2012, at 12:50 PM, Quincey Morris wrote:
> On Nov 5, 2012, at 10:03 , Paul Johnson <email@hidden> wrote:
>
>> I can sort the table by clicking on column headers, but when the
>> drawer is opened the data is unsorted.
>
> Yes, because clicking on column headers causes the table view to *create* the array of sort descriptors.
>
> What you need to do is create a property in your data model that holds the sort descriptors. Bind the array controller's Sort Descriptors binding to this property. That causes the array controller's arrangedObjects to be ordered according to the sort order in the data model property, and the table view gets this order (via arrangedObjects) without additional work.
>
> If the column header is clicked, the table view updates its own sortDescriptors property, which (via its automatically configured binding) updates the array controller's sortDescriptors property, which (via its manually configured binding) updates the data model property.
>
> Chances are you'll also want the data model property to updates user defaults when set, and initially load its value from user defaults on startup.
That's all fine and good but maybe more than needed.
If you're just looking for a quick and simple fix, you can set up an outlet from your window controller to the array controller -- there's a good chance you already have that -- and, during -awakeFromNib or -windowDidLoad, simply set the array controller's sortDescriptors property. That will set the initial sort order. Clicks in table column headers will change that property as necessary.
Cheers,
Ken
_______________________________________________
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