Re: Sorting NSTableView using Bindings
Re: Sorting NSTableView using Bindings
- Subject: Re: Sorting NSTableView using Bindings
- From: Quincey Morris <email@hidden>
- Date: Mon, 05 Nov 2012 10:50:23 -0800
On Nov 5, 2012, at 10:03 , Paul Johnson <email@hidden> wrote:
> I've selected the Table View and clicked on tab to select the Bindings
> inspector. Under the Table Content heading I found Sort Descriptors,
> and it is there that I'm focusing. (For example, I've checked the
> "Bind to" box and selected the Array Controller responsible for
> managing the table data. Then for Controller Key I have
> "arrangedObjects" and for "Model Key Path" I have the key associated
> with the first column. This isn't doing what I want; I get a
> "doesNotRecognizeSelector" exception thrown.)
It's more usual to configure the sorting in the array controller. Leave the table view Sort Descriptors binding unbound. (If it's unbound, the table view will automatically bind itself to the array controller's sortDescriptors property.)
You're getting an exception because the Sort Descriptors binding needs to get an array of NSSortDescriptor objects. What you're actually binding it to is the value of the first column.
> 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.
_______________________________________________
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