views:

277

answers:

2

Hey,

I have a problem with sorting NSTableColumn contents. In my NSTableView there are three columns: File, Size, Path. The contents are stored in NSMutableArray. Each object in this array is a NSDictionary containing three keys: file, size and path - value for each is a NSString.

In Interface Builder, in each Table Column's attributes I can choose sorting options: Selector: IB entered "compare:" which I think is ok, because I compare NSStrings. Sort Key - and that's the problem I think - I don't know what to enter here.

Any clues? If you've got questions about my code, please ask.

+1  A: 

Key is the key that you use in dictionary to retrieve the value.

In your case you have three keys: file, size and path. Select the one on which you want to sort. The key is used to retrieve value from each record to be used for sorting.

If your keys are @"file", @"size", @"path" and you want to sort on file then try to put value.file into the Sort Key field in IB.

Use keys that you use when inserting values into your NSDictionary.

stefanB
Thanks for the reply but I don't se the answer.I see this window: http://homepage.mac.com/simx/.Pictures/column_attributes.pngI have a feeling that it's easier than adding a piece of code in my app.What do you think I should enter in the 'Sort Key' field?
Yahoo
**Edited**: removed the `confusing` code example, it was to show you how it is done in code so that you see the connection between IB values and code. But seeing that it complicates things I removed it. So now it should be clear what you should insert into IB.
stefanB
Not working. I tried these too:- valueForKey:@"path"- objectForKey:@"file"- value.objectForKey:@"file" None is working.. Don't know what to do. Maybe I should create some bindings or something? From who's perspective I should write this key?
Yahoo
Ok.. I really don't know what to do. Could you post your code again? I might do sth with it.
Yahoo
A: 

So I found the complete solution.

First, go to Interface Builder. Select column that you want to sort. Go to the column's inspector and choose the first button to see the "Table Column Attributes". Set appropriate values (literally, no " or ' or @ are needed):

Sort key: file

where 'file' is the key of dictionary that contents is shown in your column.

Selector: compare:

standard sort function.

Now, save all the changes here and jump to Xcode, to the class in which is the model, source of the data shown in NSTableView. You should already know that you need two methods there:

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn
 *)tableColumn row:(NSInteger)row

these two are needed to conform the NSTableDataSource informal protocol. You can read about it at the MacDev Center.

Now all you have to do is to add a new method:

-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors

it can contain a very simple code that will do the thing:

-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors 
{       
    NSArray *newDescriptors = [tableView sortDescriptors];
    [results sortUsingDescriptors:newDescriptors]; 
    //"results" is my NSMutableArray which is set to be the data source for the NSTableView object.
    [tableView reloadData]; 
}

And that's all. It's working in my app, hope it will work in your case :D Everything should work automatically. I've just saved all files and Built the app. And it worked. :)

Site that helped me: CocoaDev: SortingNSTableView

Yahoo