简体   繁体   中英

How to sort NSArrayController using bindings from NSTableColumn?

There are similar questions to this already, but I haven't found any of them to work for me. All I want to do is re-sort the table view based on clicking the column header. And I don't want to do this programmatically because I don't think I have to. Anyways...

I have a NSTableView binded to an NSArrayController that is binded to my managedObjectContext.

For a specific NSTableColumn, I have the value binded to the Array Controller with the model key path "amount" which is an NSNumber.

I see in the attributes for this NSTableColumn, there is a "Sort Key" and a "Selector". I set the sort key to "value.amount" (not sure why, but I saw this elsewhere) and the selector defaults to "compare:" which seems fine.

Then for the NSArrayController, I see a binding called "Sort Descriptors" which I bind to "Shared User Defaults Controller". The "Controller Key" defaults to "value" (I assume this is why I used value before), and I set the "Model Key Path" to "amount.

This sounds reasonable, I suppose. But (1) it doesn't work and (2) what about when I want to sort based on two different columns? Also, (3) how do I set the default way to sort?

UPDATE:

OK, so I have the array controller binded to the managed object context. I have the table content binded to the array controller arranged objects, and selection indexes to the array controller selectionIndexes. I have a table column value binded to array controller arrange objects and the associated key. the sort key for that column is the same key with a compare: selector.

The table is not reordering when I click the headers though. Am I supposed to bind the sort descriptors of the table?

If possible, can you send me your example?

Thanks for the help,

Chet

First thing you should know is both the NSTableView and the NSArrayController can sort your items. You're trying to set a mix of both right now, and there's no point in that. For simple uses, just rely on the tableView.

So, don't bind anything in the NSArrayController. For the NSTableColumns, don't use “value.amount”, just use “amount”. “compare:” is fine for the selector.

I just created a tiny project (no code) to demonstrate this and it worked great with sorting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM