以周为单位对 NSDate 进行分组,并从最新到最旧排序
Group NSDate in weeks and order from newest to oldest
我正在为 iPhone 开发一个应用程序,我坚持到这一点:
我从对象中获取此数据:
{
date = "2014-08-04 11:21:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 14:50:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:18:57 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:19:29 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-08-04 15:25:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
}
现在我应该获取所有日期,按周分组并从最新到最旧排序...
此时我坚持如何按周对 NSDate 进行分组,当日期中的年份更多时问题就开始了,因为例如结构中的第一个元素被最后一个元素分组。
我该怎么办?谢谢
编辑:
这是我用来按周分组的代码:
NSMutableArray *weekArray = [NSMutableArray array];
for (int i = 0; i<52; i++) {
[weekArray addObject:[NSMutableArray array]];
}
for (int i = 0; i<_array.count; i++) {
NSDictionary *dict = [_array objectAtIndex:i];
NSDate *date = [dict objectForKey:@"date"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];
NSMutableArray *innerArray = weekArray[[dateComponents weekOfYear] -1];
[innerArray addObject:dict];
}
for (int i = 0; i<weekArray.count; i++) {
if ([[weekArray objectAtIndex:i] count] > 0) {
NSNumber *iNum = [NSNumber numberWithInt:i+1];
[_mutableDictionary setObject:[weekArray objectAtIndex:i] forKey:iNum];
}
}
_mutableDictionary 是:
31 = (
{
date = "2015-07-31 16:19:29 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:19:23 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:18:57 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 14:50:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
}
);
32 = (
{
date = "2015-08-04 15:25:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2014-08-04 11:22:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
}
);
31 和 32 是一年中的周数
我认为这不符合我的目标...
以正确的方式对这个 NSDate 进行分组后,我必须有一个结构允许我每周在 UITableView 中创建 n 个部分
您需要根据年份和年份中的星期进行分组。您还需要避免对 52 个空数组进行硬编码。相反,使用数组字典,其中键代表年和周,值是数组。
尝试这样的事情:
NSMutableDictionary *weeksDictionary = [NSMutableDictionary dictionary];
NSCalendar *calendar = [NSCalendar currentCalendar];
for (NSDictionary *dict in _array) {
NSDate *date = [dict objectForKey:@"date"];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];
NSInteger year = dateComponents.year;
NSInteger week = dateComponents.weekOfYear;
NSInteger index = year * 100 + week;
NSNumber *key = @(index);
NSMutableArray *weekArray = weeksDictionary[key];
if (!weekArray) {
weekArray = [NSMutableArray array];
weeksDictionary[key] = weekArray;
}
[weekArray addObject:dict];
}
这为每个 year/week 具有日期的数组提供了一个数组。
要将其与 table 视图一起使用,请对字典的键进行排序以获得按顺序排列的 year/week 个值的数组。然后你可以拆分每个键得到年和周:
NSNumber *key = sortedKeysArray[indexPath.section];
NSInteger value = key.integerValue;
NSInteger year = value / 100;
NSInteger week = value % 100;
我正在为 iPhone 开发一个应用程序,我坚持到这一点:
我从对象中获取此数据:
{
date = "2014-08-04 11:21:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 14:50:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:18:57 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:19:29 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-08-04 15:25:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
}
现在我应该获取所有日期,按周分组并从最新到最旧排序...
此时我坚持如何按周对 NSDate 进行分组,当日期中的年份更多时问题就开始了,因为例如结构中的第一个元素被最后一个元素分组。
我该怎么办?谢谢
编辑:
这是我用来按周分组的代码:
NSMutableArray *weekArray = [NSMutableArray array];
for (int i = 0; i<52; i++) {
[weekArray addObject:[NSMutableArray array]];
}
for (int i = 0; i<_array.count; i++) {
NSDictionary *dict = [_array objectAtIndex:i];
NSDate *date = [dict objectForKey:@"date"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];
NSMutableArray *innerArray = weekArray[[dateComponents weekOfYear] -1];
[innerArray addObject:dict];
}
for (int i = 0; i<weekArray.count; i++) {
if ([[weekArray objectAtIndex:i] count] > 0) {
NSNumber *iNum = [NSNumber numberWithInt:i+1];
[_mutableDictionary setObject:[weekArray objectAtIndex:i] forKey:iNum];
}
}
_mutableDictionary 是:
31 = (
{
date = "2015-07-31 16:19:29 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:19:23 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 16:18:57 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2015-07-31 14:50:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
}
);
32 = (
{
date = "2015-08-04 15:25:26 +0000";
idChallenger = "-1";
index = "-1";
time = "-1";
},
{
date = "2014-08-04 11:22:40 +0000";
idChallenger = 43;
index = "-1";
time = "-1";
}
);
31 和 32 是一年中的周数
我认为这不符合我的目标...
以正确的方式对这个 NSDate 进行分组后,我必须有一个结构允许我每周在 UITableView 中创建 n 个部分
您需要根据年份和年份中的星期进行分组。您还需要避免对 52 个空数组进行硬编码。相反,使用数组字典,其中键代表年和周,值是数组。
尝试这样的事情:
NSMutableDictionary *weeksDictionary = [NSMutableDictionary dictionary];
NSCalendar *calendar = [NSCalendar currentCalendar];
for (NSDictionary *dict in _array) {
NSDate *date = [dict objectForKey:@"date"];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];
NSInteger year = dateComponents.year;
NSInteger week = dateComponents.weekOfYear;
NSInteger index = year * 100 + week;
NSNumber *key = @(index);
NSMutableArray *weekArray = weeksDictionary[key];
if (!weekArray) {
weekArray = [NSMutableArray array];
weeksDictionary[key] = weekArray;
}
[weekArray addObject:dict];
}
这为每个 year/week 具有日期的数组提供了一个数组。
要将其与 table 视图一起使用,请对字典的键进行排序以获得按顺序排列的 year/week 个值的数组。然后你可以拆分每个键得到年和周:
NSNumber *key = sortedKeysArray[indexPath.section];
NSInteger value = key.integerValue;
NSInteger year = value / 100;
NSInteger week = value % 100;