I’ve discovered that some people may want to programatically remove the sort descriptors for their tableview:
This is easy to do with a little code:
- (IBAction)removeSorting:(id)sender { [tableView setSortDescriptors:[NSArray array]]; }
However, you may want your users to be able to do it. It is fairly simple to add a button in the corner view of your table that does this:
In your awakeFromNib method you can easily add a bit of code that adds this button and invokes the above “removeSorting:†method:
NSRect buttonFrame = [[tableView cornerView] frame]; NSButton *button = [[NSButton alloc] initWithFrame:buttonFrame]; [button setBezelStyle:NSSmallSquareBezelStyle]; [button setTitle:@“Xâ€]; [button setTarget:self]; [button setAction:@selector(removeSorting:)]; [tableView setCornerView:button]; [button release];
Have Fun, and note that I recommend that you don’t have auto-scrollbar hiding turned on when you use this (otherwise, the corner view won’t be visible!)
-corbin
Next in General
There is something that bothers me about having the [X] all the way to the side like that. It is clever, and I know that clicking on that [X] while one of the columns is selected deletes the sort ordering on that column, but bothers me that if you want to delete the sort ordering on that far-left-hand column, you select it and then go all the way to the right.
It seems to me there is another way. Say the column is sorting ascending. The icon shown is the right-side-up triangle. Click on the column header. It changes to descending and the icon shown is the upside-down triangle. Click on it again and the sort is removed and the icon shown is a square. I have seen this done before.
Or am I missing something?
Ray — clicking on the [x] would remove all the sorting’s, and not just the selected column. There may be sub-sortings going on, which aren’t represented by any UI at all.
But, as you have noticed, it is a little strange to have an X in that location. Doing the click again to remove the sorting would be nice, and fairly easy to implement by a developer. Except, instead of a square it would probably just go back to not having a sort indicator on that column (and/or the previously sorted column becomes the main sorted column again).
Geez Corbin,
I was just passing by and noticed this post. I don’t know, but if I was you, I would really, really long for my Delphi programming days; That code is just shocking!
No matter how many times I’ve tried to make myself interested in Mac Apps development, it’s the absense of a proper (Delphi VCL like) framework that really spoils it for me.