zoukankan      html  css  js  c++  java
  • iOS开发 服务器请求出来的数据按日期重新分组

    在APP开发中经常碰到这种情况,后台给的数据需要我们按照某种条件分组。比如我的项目中:需要把数组中的数据按照时间分类分组,同一天的数据放在一起比如8-20号的数据分一组,8-21号的数据分一组。代码如下:

    #import "ViewController.h"
    #import "model.h"
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    {
        NSMutableArray *userModelArr;
        NSMutableArray *tempArray;
        NSMutableArray *_dateArr;
        NSMutableArray *timeArr;
        NSArray  * arrrrrrrrrr;
        
    }
    @property(nonatomic,strong)UITableView *tableView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       self.title = @"会议";
    
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        [self.view addSubview:_tableView];
        
       [self shuju];
        
        
    
    }
    -(void)shuju
    {
        tempArray = [NSMutableArray array];
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:@"http://dev.brc.com.cn:8085/api/meeting/list?access_token=1b5e8731e5ff44928991b3098fe52464&type=32&showrows=10&page=1"];
        // 通过URL初始化task,在block内部可以直接对返回的数据进行处理
        NSURLSessionTask *task =[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
            
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]];
            
            NSLog(@"999%@22",dic);
            
            id jsonArr = dic[@"data"];
            
            NSMutableArray *allTimeArr = [NSMutableArray array];
            for (NSDictionary *ordersDic in jsonArr) {
                
                //1.取出所有出现得时间
                [allTimeArr addObject:[ordersDic[@"starttime"] substringToIndex:10]];
            }
           
        arrrrrrrrrr = [self arrayWithMemberIsOnly:allTimeArr];
            NSLog(@"%@",arrrrrrrrrr);
            
            for (NSString *nowTim in arrrrrrrrrr) {
                
                NSMutableArray *arr = [[NSMutableArray alloc] init];
                for (NSDictionary *ordersDicTwo in jsonArr) {
                    
                    NSString *twoTim = [ordersDicTwo[@"starttime"] substringToIndex:10];
                    if([twoTim isEqualToString:nowTim]){
                        //2.将每个字典保存在模型数组中
                        model *tianjianModel = [[model alloc]init];
                        [tianjianModel setValuesForKeysWithDictionary:ordersDicTwo];
                        [arr addObject:tianjianModel];
                        
                    }
                    
                }
                
                
                [tempArray addObject:arr];
            }
            
            [_tableView reloadData];
           }];
        
        [task resume];
    //    });
    
    }
    
    // 告诉TableView对应的Section要显示几行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
        NSArray *arr = tempArray[section];
    
        return  arr.count;
    }
    
    // 告诉TableView要显示什么内容,当Cell显示的时候就会调用
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    
        
        NSArray *arr = tempArray[indexPath.section];
        model *cellModel = arr[indexPath.row];
        
        cell.textLabel.text = cellModel.title;
        
        return cell;
    }
    // 返回分组(Section)数 Section
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return tempArray.count;
    }
    // 返回组头部的Title
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    
        return [NSString stringWithFormat:@"第%ld组", section];
    }
    // 返回组头部(段头)的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 30;
    }
    
    //去除数组中重复的
    -(NSArray *)arrayWithMemberIsOnly:(NSArray *)array
    {
        NSMutableArray *categoryArray =[[NSMutableArray alloc] init];
        for (unsigned i = 0; i < [array count]; i++) {
            @autoreleasepool {
                if ([categoryArray containsObject:[array objectAtIndex:i]]==NO) {
                    [categoryArray addObject:[array objectAtIndex:i]];
                }
            }
        }
        return categoryArray;
    }
    

     

  • 相关阅读:
    邮件发送
    生成缩略图
    截取字符串,中文安两个字符处理
    获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址
    文件操作
    MD5加密 和 自定义加密解密
    Html标签替换(过滤掉html特殊符号)
    .net core 自定义中间件
    querySelector和querySelectorAll
    CSS下背景属性background的使用方法
  • 原文地址:https://www.cnblogs.com/laolitou-ping/p/6900437.html
Copyright © 2011-2022 走看看