iOS大量数据按时间分组

有这样一个需求,按月显示账单,每个月是一个组,组头是显示月份,列表显示每笔交易数据,这个时候后端无法直接返回这种数据(难道每个月的数据刚好是一页?),所以只能按分页去请求对应那一页的数据(数据带上时间戳就行了),前端处理思路,循环遍历本地所有数据(到加载当前页和之前所有页数据),按时间戳转成年月,加到对应那条数据模型中去,并且用这个带有年月的模型替换之前的数据模型,并且把这个年月放的一个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++) {

        //时间

扫描二维码关注公众号,回复: 5454979 查看本文章

        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;

                                                        }];

}

猜你喜欢

转载自www.cnblogs.com/yeas/p/10491419.html