开发者

Sorting a column in NSOutlineView that is bound to NSTreeController

I have an NSOutlineView that is bound to an NSTreeController. In Interface builder, I have bound each column of NSOutlineView to the tree controller with the Controller key "arrangedObjects"开发者_开发技巧 and model key path as the entity attribute or a method in my entity class.

Now all other columns sort perfectly except for one special column. The special column has a model key path bound to a method that is declared in my Entity class. This method depending on some condition in my code will return either NSString or NSDictionary. When it returns an NSDictionary, the delegate method:

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item 

parses the NSDictionary and returns a double value for that cell. Also note that the cell in this case is derived from NSCell and displays a progress bar.

In short, my special column will display a mix of progress bars and strings depending on the situation.

I would like to implement sorting so that all progress bars stay together and the strings are sorted alphabetically.


Perhaps you will have some luck if you try setting the table column's sort descriptor to a descriptor you create with a comparator that looks at the objects' classes. (You could also try filling in the "Sort Key" in IB, using the "class" key, but I think this might be less likely to work...)


Well i found a solution to this myself and just 2 minutes after posting my query :) The solution was to add a custom sort key in IB and define a method with that key name in my Entity class that returns an NSInteger. Since i want all NSDictionary objects to stay together and all NSStrings to be sorted, i return appropriate integer based on the object type. Note for anyone who might be stuggling with sort not working: Make sure binding for sortDescriptor is enabled in IB.


try

NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
                                 initWithKey:NULL
                                 ascending:YES
                                 selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
[[myOutlineView tableColumnWithIdentifier:@"Name"]setSortDescriptorPrototype:sorter];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜