1

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

(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];

}
4

1 回答 1

0

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

于 2013-10-30T01:22:08.910 回答