0

我有一个数据列表,我可以通过点击列标题进行排序。唯一的问题是,它会显示如:NSTableView排序顺序

10A 
11A 
12A 
1A 
2A 
3A 

etc... 

理想在哪里,我想它进行排序,如:

1A 
2A 
3A 
4A 
5A 
6A 
7A 
8A 
9A 
10A 
11A 

etc... 

有什么办法来解决这个默认的排序操作?

这是我的排序描述符属性:

Sort Descriptors Properties

@Monolo:

enter image description here

self.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"self" 
                 ascending:YES 
                 comparator:^NSComparisonResult(id obj1, id obj2) { 
                  NSLog(@"COMPARATOR!"); 
                  return [obj1 compare:obj2 
                     options:NSNumericSearch]; 
                 }]]; 
+0

http://stackoverflow.com/questions/8242735/how-to-sort-array-controller-alphabetically-with-numbers-last-in-objective-c – 2013-02-13 11:27:28

回答

0

一般来说,那种排序,你都在寻找可通过获得NSStringcompare:options:方法,所以如果你在代码中构造一个排序描述符并以编程方式或通过绑定nib文件将它们放入数组控制器中,则应该获得所需的内容。

建立一种描述符阵列,比如像这样的:

// AppDelegate.h: 

@property (strong) NSArray *sortDescriptors; 


//AppDelegate.m: 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    self.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"self" 
               ascending:YES 
               comparator:^NSComparisonResult(id obj1, id obj2) { 
                return [obj1 compare:obj2 
                   options:NSNumericSearch]; 
               }]]; 
} 

因此,而不是绑定到标准用户默认控制器,我们应该绑定到应用程序委托(或者你更喜欢一些其他的对象持有这些助手对象),并获得你之后的行为。使用上面的代码片段,绑定的模型关键路径是sortDescriptors

+0

我是否更改表的排序描述符,或阵列控制器? – 2013-02-13 12:18:12

+0

当我使用该代码时,结果是意外的。我编辑了原始帖子以包含sceenshot, – 2013-02-14 18:06:11

+0

我可以确认NSLog语句没有被调用,但是appdidfinishlaunching fucntion是。这是NSLog中的代码(附带提问)。 – 2013-02-14 18:23:58