日付と名前の 2 つの列を持つテーブルがあります。このコードを使用して要素を並べ替えます。
-(id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex {
NSDictionary *itemDictionary = [itemListArray objectAtIndex:rowIndex];
if (aTableColumn == itemName)
return [itemDictionary valueForKey:@"ItemNameBrowser"];
else if (aTableColumn == dateCare)
{
NSDate *date_to_set;
if ([[itemDictionary valueForKey:@"ItemdateCare"] isKindOfClass:[NSString class]])
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
date_to_set = [dateFormatter dateFromString:[itemDictionary valueForKey:@"ItemdateCare"]];
}
else
{
date_to_set = [itemDictionary valueForKey:@"ItemdateCare"];
}
return date_to_set;
}
else
return nil;
}
//sorting table view
-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors
{
[itemListArray sortUsingDescriptors: [tableView sortDescriptors]];
[tableView reloadData];
}
-(void)addItem {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
//Valori da aggiungere
NSString *newItemNameBrowser = @"Item";
NSString *newItemDateCare = [NSDate date];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setValue:newItemNameBrowser forKey:@"ItemNameBrowser"];
[newItem setValue:newItemDateCare forKey:@"ItemdateCare"];
[itemListArray addObject:newItem];
[tableList reloadData];
[dateFormatter release];
}
MainMenu.xib の列は次のとおりです。
Sort Key ItemName
Selector compare:
と
Sort Key ItemDate
Selector compare:
列名は大丈夫です。日付を正しく並べ替えないため、問題は日付列の並べ替えです。要素は数字のみと見なされます。
手伝ってくれてありがとう!