Reputation: 9120
I have NSTableView bind it to NSArrayController. I'm sorting the NSTableView and it works fine but when I click on the row of NSTableView I get different value is like the NSArrayController is not been sorted.
Here is how I'm sorting the NSTableView:
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"names" ascending:YES];
[self.names setSortDescriptors:[NSArray arrayWithObject:sd]];
For example when I click on Name1 I get the value of the Name2. What I need is to match the values of NSTableView with the NSArrayController. Any of you knows what I'm doing wrong? or how can sort both the NSTableView and the NSArrayController ?
I'll really appreciate your help
Upvotes: 0
Views: 835
Reputation: 90521
when I click on Name1 I get the value of the Name2
How do you get the value?
I suspect you're looking in the wrong place.
First, you should bind the table view's selectionIndexes
binding to the array controller's selectionIndexes
property. Likewise, you should bind the table view's sortDescriptors
binding to the array controller's sortDescriptors
property.
It's not clear what self.names
refers to in your code. Is that the table view or the array controller? Personally, I would set the sort order of the table view by setting the array controller's sortDescriptors
.
Anyway, after all that is set up, when you want to get the selected object(s), you have to consult the array controller, not the model that may be providing the content of the array controller. (You didn't actually say, but I'll assume your array controller's contentArray
binding is bound to an indexed collection property in your model.)
First, you can simply query its selectedObjects
property.
Alternatively, if you're starting from a row index, you must use that to index into its arrangedObjects
.
You must not simply take a row or a selection index and index into the indexed collection property of your model. The array controller has rearranged the objects before providing them to the table view (based on the sort descriptors and, possibly, a filter predicate). So, the indexes no longer correspond to the original indexed collection in the model.
Upvotes: 1