简体   繁体   中英

Sort order arrow is displayed for all NSTableView headers?

I've changed the sort descriptor for my NSTableView columns. The new descriptor is rockinggood, but now all headers have the sort arrow displayed.

在此处输入图片说明

在此处输入图片说明

Why? How can I leave the sort arrow for the header belonging to the column I'm currently sorting by ? This is the code (old descriptor is commented out):

        //Old descriptor
        //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:metadata.columnSortKeyPath ascending:NO selector:metadata.columnSortSelector];

        //New descriptor
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2)  {

            if ([obj1 isKindOfClass:[CBFolder class]])  {
                return NSOrderedSame;
            }

            if (currentClickedHeaderTableColumn) {
                CBTableColumnMetadata metadata = [(CBApplicationDelegate*)[NSApp delegate] tableColumnMetadataForKey:currentClickedHeaderTableColumn.identifier];
                return [[obj1 valueForKey:metadata.columnSortKeyPath] performSelector:metadata.columnSortSelector withObject:[obj2 valueForKey:metadata.columnSortKeyPath]];
            }

            return NSOrderedSame;
        }];

I believe how it works is that the table view has its sortDescriptors property, which is the array of sort descriptors in effect (primary, secondary, etc.).

A column shows the sort indicator if its sortDescriptorPrototype matches the first element of the table view's sortDescriptors .

You appear to be using the same sort descriptor for all of your columns. Therefore, they all match and all show the sort indicator.

I think your sort descriptor is inherently broken because of its reliance on the currentClickedHeaderTableColumn . That suggests that each column's sort order changes as the "clicked" column changes, which is just broken. A given column's sort order (the order it would be in if the table were sorted by that column) shouldn't change depending on irrelevant state like that.

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