2017-10-06 49 views
0

我無法使用多個排序描述符對CoreData App的基於視圖的tableview進行排序。 我有2列,我的第一列有「bank」和「accountNickName」作爲值,我的第二列有「bankAccount」。
我想按「銀行」,然後「accountNickName」和「bankAccount」進行排序。如果我點擊第一列。
我想按「bankAccount」,然後選擇「銀行」,然後選擇「accountNickName」。
如果我點擊第二列。
創建sortDescriptors的數組,這個綁定到我的arraycontroller不起作用:使用多種排序描述符對基於視圖的tableview進行排序不起作用

sortForStockInfo = [ NSArray arrayWithObjects: 
          [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:YES selector:@selector(compare:)], 
          [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:YES selector:@selector(compare:)], 
          [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:YES selector:@selector(compare:)], 
          nil]; 

的tableview中有綁定到陣列控制器的「排序描述符」,「排序描述符」。我想,這就是我必須要做的。但它不起作用。我錯過了什麼?足夠奇怪:如果我使用相同的方法,但我填寫了Sortkey,Selector和Order的列屬性,它僅對一個方面進行排序(例如,銀行或帳戶,accountNickName保持未排序)。因爲我只能爲每列定義一個標準。

回答

1

sortDescriptors是一個數組,點擊列的sortDescriptor插入到索引0處。當用戶點擊列0然後點擊列1時,排序順序爲column1,column0。 實施陣列控制器的代理方法- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn並設置sortDescriptors。例如:

- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn { 
    NSArray *sortDescriptors = self.arrayController.sortDescriptors; 
    if (sortDescriptors && [sortDescriptors count] > 0) { 
     NSSortDescriptor *firstSortDescriptor = sortDescriptors[0]; // sort descriptor of the clicked column 
     BOOL ascending = firstSortDescriptor.ascending; 
     if ([firstSortDescriptor.key isEqualToString:@"bank"]) { 
      self.arrayController.sortDescriptors = @[ 
       [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)], 
       [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)], 
       [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)]]; 
     } 
     else 
      if ([firstSortDescriptor.key isEqualToString:@"account"]) { 
       self.arrayController.sortDescriptors = @[ 
        [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)], 
        [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)], 
        [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)]]; 
      } 
    } 
} 
+0

完美!你的代碼只是缺失的鏈接。 –