Removing sort descriptors on a sorted NSTableView / NSOutlineView

Cocoa, General

I’ve discovered that some people may want to programatically remove the sort descriptors for their tableview:

Removesortdesc1

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:

Removesortdesc2

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

Tags:


Subscribe
Notify of
guest

3 Comments
Inline Feedbacks
View all comments
Ray

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?

wpSlider

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.

Subscribe to new posts:

You'll get an email whenever a I publish a new post to my blog and nothing more. -- Corbin

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2024 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

70 queries. 0.173 seconds.

Log in