1. 程式人生 > >iOS大量數據按時間分組

iOS大量數據按時間分組

ons nbsp one animation pre arm mov interval enum

有這樣一個需求,按月顯示賬單,每個月是一個組,組頭是顯示月份,列表顯示每筆交易數據,這個時候後端無法直接返回這種數據(難道每個月的數據剛好是一頁?),所以只能按分頁去請求對應那一頁的數據(數據帶上時間戳就行了),前端處理思路,循環遍歷本地所有數據(到加載當前頁和之前所有頁數據),按時間戳轉成年月,加到對應那條數據模型中去,並且用這個帶有年月的模型替換之前的數據模型,並且把這個年月放的一個set裏面,遍歷set 按set裏面的年月創建謂詞篩選器,把總數據用過濾方法得到新的數組,在添加的最終的數組sectionArary中

/*******************************************分組相關********************************************************/

- (void)sectionWithListForDate

{

[self.sectionArary removeAllObjects];

NSMutableSet *set=[NSMutableSet set];

NSInteger count = self.listData.data.count;

for (NSInteger i =0 ;i < count; i++) {

//時間

NSMutableDictionary * modelDic = self.listData.data[i];

NSString * timeStamp = STR_OBJ(modelDic[@"created_at"]);

NSDate * time = [NSDate dateWithTimestamp:timeStamp];

NSString * yearMonth = [NSString stringWithFormat:@"%ld-%02ld", [time yearIndex],[time monthIndex]] ;

modelDic[@"year_month"] = yearMonth;

[self

.listData.data replaceObjectAtIndex:i withObject:modelDic];

[set addObject:yearMonth];//利用set不重復的特性,得到有多少組,根據數組中的created_at字段

}

// NSMutableSet *set=[NSMutableSet set];

// [self.listData.data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

// //時間

// NSDictionary * modelDic = obj;

// NSString * timeS = STR_OBJ(modelDic[@"created_at"]);

//

// [set addObject:timeS];//利用set不重復的特性,得到有多少組,根據數組中的created_at字段

// }];

[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {//遍歷set數組

MLOG(@"obj=%@",obj);

NSString * yearMonth = obj;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"year_month = %@", yearMonth];//創建謂詞篩選器

NSArray *groupList = [self.listData.data filteredArrayUsingPredicate:predicate];//用數組的過濾方法得到新的數組,在添加的最終的數組sectionArary中

NSMutableDictionary * mothDate = [NSMutableDictionary dictionary];

[mothDate setValue:groupList forKey:@"list"];

[mothDate setValue:yearMonth forKey:@"time"];

[self.sectionArary addObject:mothDate ];

NSInteger sectionIndex = [self.sectionArary indexOfObject:mothDate];

//按年月去請求對應月份的的相關信息及數據

[self requsetMonthInAndOut:yearMonth sectionIndex:sectionIndex];

}];

[self.sectionArary sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {

NSString * time1 = obj1[@"time"];

NSDate * date1 = [NSDate ym_dateFromString:time1 format:@"yyyy-MM"];//[NSDate dateWithString:time1 format:@"yyyy年MM月"];

long long timeSp1 = [[NSNumber numberWithDouble:[date1 timeIntervalSince1970]] longLongValue];

NSString * time2 = obj2[@"time"];

NSDate * date2 = [NSDate ym_dateFromString:time2 format:@"yyyy-MM"];//[NSDate dateWithString:time2 format:@"yyyy年MM月"];

long long timeSp2 = [[NSNumber numberWithDouble:[date2 timeIntervalSince1970]] longLongValue];

return timeSp1 < timeSp2;

}];

}

- (void)requsetMonthInAndOut:(NSString*)yearMonth sectionIndex:(NSInteger)sectionIndex

{

// NSString * timeStr = [yearMonth stringByReplacingOccurrencesOfString:@"年" withString:@"-"];

// timeStr = [timeStr stringByReplacingOccurrencesOfString:@"月" withString:@""];

NSMutableDictionary * param = [NSMutableDictionary dictionary];

[param setObject:NON(self.shopIdStr) forKey:@"shop_id"];

[param setObject:NON(yearMonth) forKey:@"month"];

@weakify(self)

[self showHUD];

[[RequestClient sharedInstance] requestTransferRecordMonthInfo:param

success:^(NSURLSessionDataTask *operation, id responseObject) {

@strongify(self)

[self hideHUD];

NSMutableDictionary * monthData = self.sectionArary[sectionIndex];

monthData[@"in"] = responseObject[@"in"];

monthData[@"out"] = responseObject[@"out"];

[self.sectionArary replaceObjectAtIndex:sectionIndex withObject:monthData];

//刷新

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationNone];

[self endRefreshing];

_isFirstLoad = NO;

}

failure:^(NSURLSessionDataTask *operation, NSError *error) {

@strongify(self)

[self hideHUD];

[self showError:error];

[self endRefreshing];

_isFirstLoad = NO;

}];

}

iOS大量數據按時間分組