排序标题

时间:2013-10-28 00:20:39

标签: objective-c macos cocoa nstableview

我有一个NSTable填充了数据。我使用以下代码对列进行了排序。问题是,当我想要数字时,它按字母顺序对Number列进行排序。

(1,100,101,2,20,21,3,30,31................)

我将数据输入为数字(@1, @2, @3)而非字符串(@"1", @"2", @"3")

// THIS WILL SORT ALL COLUMNS //

- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn     *)tableColumn

{

NSTableColumn *NumberColumn = [tableView tableColumnWithIdentifier:@"No."];
NSSortDescriptor *NumberSortDescriptor = [NSSortDescriptor
                                    sortDescriptorWithKey:@"No."
                                    ascending:YES
                                    selector:@selector(compare:)];
[NumberColumn setSortDescriptorPrototype:NumberSortDescriptor];

NSTableColumn *CardNameColumn = [tableView tableColumnWithIdentifier:@"Card Name"];
NSSortDescriptor *CardNameSortDescriptor = [NSSortDescriptor
                                      sortDescriptorWithKey:@"Card Name"
                                      ascending:YES
                                      selector:@selector(compare:)];
[CardNameColumn setSortDescriptorPrototype:CardNameSortDescriptor];

NSTableColumn *TypeColumn = [tableView tableColumnWithIdentifier:@"Type"];
NSSortDescriptor *TypeSortDescriptor = [NSSortDescriptor
                                    sortDescriptorWithKey:@"Type"
                                    ascending:YES
                                    selector:@selector(compare:)];
[TypeColumn setSortDescriptorPrototype:TypeSortDescriptor];

NSTableColumn *RarityColumn = [tableView tableColumnWithIdentifier:@"Rarity"];
NSSortDescriptor *RaritySortDescriptor = [NSSortDescriptor
                                      sortDescriptorWithKey:@"Rarity"
                                      ascending:YES
                                      selector:@selector(compare:)];
[RarityColumn setSortDescriptorPrototype:RaritySortDescriptor];

NSTableColumn *UnlimitedColumn = [tableView tableColumnWithIdentifier:@"Unlimited"];
NSSortDescriptor *UnlimitedSortDescriptor = [NSSortDescriptor
                                      sortDescriptorWithKey:@"Unlimited"
                                      ascending:YES
                                      selector:@selector(compare:)];
[UnlimitedColumn setSortDescriptorPrototype:UnlimitedSortDescriptor];

NSTableColumn *FirstEditionColumn = [tableView tableColumnWithIdentifier:@"1st Edition"];
NSSortDescriptor *FirstEditionSortDescriptor = [NSSortDescriptor
                                      sortDescriptorWithKey:@"1st Edition"
                                      ascending:YES
                                      selector:@selector(compare:)];
[FirstEditionColumn setSortDescriptorPrototype:FirstEditionSortDescriptor];

}

- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray
                                                             *)oldDescriptors
{
[BaseSet sortUsingDescriptors: [tableView sortDescriptors]];
[tableView reloadData];

}

1 个答案:

答案 0 :(得分:0)

我发现我的所有代码都是正确的。问题是我的应用程序在终止时保存了我的数组,并且在我将字符串转换为数字之前保存了它。