按照NSDatesorting表格部分

如何sortingNSDate ,使得如果date的时间是星期天早上的05:00,那么它将在星期六“晚上”停留,但最后在列表中?

我按date从JSON数据sorting我的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(星期天)和比赛开始01:35上午所以游戏显示在电视上星期天早晨,但我想在星期六。 部分…..所以实际上我只是从上午5点到上午5点而不是00:00 – 00:00

在这里输入图像说明

您应该确定一个timeZoneNSDateFormatter一起使用,以在构造节标题时使用。

例如,下面我显示了一系列按照when属性sorting的事件,不同之处在于节标题将在某个预定的时区,而不是我设备的特定时区。 因此,我首先使用适当的NSTimeZone的date格式化程序构build支持我的表的模型(一个Section对象数组,每个数组有一个items数组):

 // Given some array of sorted events ... NSArray *sortedEvents = [events sortedArrayUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"when" ascending:YES]]]; // Let's specify a date formatter (with timezone) for the section headers. NSDateFormatter *titleDateFormatter = [[NSDateFormatter alloc] init]; titleDateFormatter.dateStyle = NSDateFormatterLongStyle; titleDateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; // use whatever you want here; I'm just going to figure out sections in GMT even though I'm currently in GMT-5 // Now let's build our array of sections (and the list of events in each section) // using the above timezone to dictate the sections. self.sections = [NSMutableArray array]; NSString *oldTitle; for (Event *event in sortedEvents) { NSString *title = [titleDateFormatter stringFromDate:event.when]; // what should the section title be if (![oldTitle isEqualToString:title]) { // if different than last one, add new section [self.sections addObject:[Section sectionWithName:title]]; oldTitle = title; } [[(Section *)self.sections.lastObject items] addObject:event]; // add event to section } 

但是,当我显示单元格内容时,如果我不触摸timeZone参数,它将默认显示当前时区中的实际时间。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"EventCell"; EventCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; Section *section = self.sections[indexPath.section]; Event *event = section.items[indexPath.row]; // This formatter really should be class property/method, rather than instantiating // it each time, but I wanted to keep this simple. But the key is that // I don't specify `timeZone`, so it defaults to current timezone. NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.timeStyle = NSDateFormatterMediumStyle; cell.eventTimeLabel.text = [formatter stringFromDate:event.when]; // do additional cell population as you see fit return cell; } 

对于部分头文件,在构build支持此表视图的模型的例程中(即使用硬编码的时区)使用该部分名称。

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [(Section *)self.sections[section] sectionName]; } 

正如您所看到的那样,时间在当前时区中列出,但是它们按照构build部分列表的代码中指定的预定NSTimeZone分组。

在这里输入图像说明

显然,我只是使用了GMT作为我的时区,但是您可能想使用时区作为赛事的场地或者NBA篮球的时区,美国的一些时区。 但希望这个说明了基本的想法。 创build节时使用一个时区,显示实际时间时使用默认时区。