【发布时间】:2015-03-01 12:42:42
【问题描述】:
我如何对NSDate 进行排序,以便如果某个日期的时间是周日早上 05:00,它会停留在周六“晚上”,但在列表中排在最后?
我从 JSON 数据中按日期对我的 tableview 部分进行排序
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"fodboldStream", @"command", nil] onCompletion:^(NSDictionary *json) {
//got stream
//NSLog(@"%@",json);
[[API sharedInstance] setSoccer: [json objectForKey:@"result" ]];
games = [json objectForKey:@"result"];
sections = [NSMutableDictionary dictionary];
for (NSDictionary *game in games) {
NSNumber *gameType = game[@"dato"];
NSMutableArray *gamesForType = sections[gameType];
if (!gamesForType) {
gamesForType = [NSMutableArray array];
sections[gameType] = gamesForType;
}
[gamesForType addObject:game];
}
// NSLog(@"%@",sections);
[fodboldTabel reloadData];
}];
这是我的部分标题:
- (NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
//...if the scetions count is les the 1 set title to opdating ...
if ([[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count] < 1) {
return @"Opdater....";
} else {
// ..........seting tabelheader titel to day and date..................
NSString *str = [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
date = [dateFormatter dateFromString:str];
dateFormatter.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"];
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
dateFormatter.dateFormat=@"EEEE";
NSString * dayString = [[dateFormatter stringFromDate:date] capitalizedString];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger year = [components year];
// NSInteger month=[components month]; // if necessary
NSInteger day = [components day];
//NSInteger weekday = [components weekday]; // if necessary
NSString *sectionLbl = [NSString stringWithFormat: @"%@ %li %@ %li", dayString, (long)day, monthString, (long)year];
return sectionLbl;
}
}
这是一张图片,你可以看到标题写着 søndag(星期日),比赛从上午 1:35 开始,所以比赛在周日早上的电视上播放,但我希望在周六播出。部分.....所以实际上我只是看着“一天”从早上 5 点到凌晨 5 点而不是 00:00 - 00:00
【问题讨论】:
-
"...如果某个日期的时间是星期六早上 05:00,它会停留在星期六的“晚上”,但在列表中是最后一个。”目前还不清楚你在问什么。也许只是向我们展示一个示例(a)输入数据的样子; (b) 你希望输出的样子。
-
您有问题吗?
-
我如何让“一天”从早上 5 点到凌晨 5 点而不是 00:00 - 00:00
-
与那些时代相比。或者虚设一个相差 5 小时的时区。
标签: ios objective-c uitableview nsdate