Objective c 分类表日期目标-C
我有一个表,有两列:日期和名称。 我使用此代码对元素进行排序:Objective c 分类表日期目标-C,objective-c,macos,sorting,date,nstableview,Objective C,Macos,Sorting,Date,Nstableview,我有一个表,有两列:日期和名称。 我使用此代码对元素进行排序: -(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { NSDictionary *itemDictionary = [itemListArray objectAtIndex:rowIndex]; if (aTableCol
-(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:
及
列名称是确定的。
问题是对日期列进行排序,因为没有正确地对日期进行重新排序。
元素仅被视为数字
谢谢你的帮助 我假设您正在将日期作为字符串存储在
itemListArray
中。不要。将它们存储为NSDATE,然后它们将正确排序
您需要将NSDateFormatter附加到NSTableColumn文本字段单元格,以将其转换为字符串进行查看。我添加了代码。我使用这个:NSDate*newItemDate=[NSDate-date];如我所说,要添加元素,请将日期存储为
NSDates
,而不是字符串。仅在需要显示的位置将其转换为字符串,这可以通过文本字段单元格上的NSDateFormatter
自动完成。我更新了代码,现在它可以工作,但不使用NSDateFormatterShortStyle。我想要日期为DD/MM/YYYY HH.MM.SS。我不明白哪里错了。你能帮助我吗?非常感谢。@Joannes:你用的是中等风格,不是短风格。您正在使用的日期格式化程序是您在表:objectValueForTableColumn:
中创建的,而不是附加项:
P:对,我设置了NSDateFormatterShortStyle,但保留了“中欧夏季时间”。我可以删除它吗?
Sort Key ItemDate
Selector compare: