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