zoukankan      html  css  js  c++  java
  • iOS tableView 数据处理,数据分类相同数据整合、合并计算总数总价

    //

    数据下载得到数组数据

     modelArray = [MZPriceModel mj_objectArrayWithKeyValuesArray:data[@"info"]];
            
            if (modelArray.count>0) {
                
                
                // 分类的数组 
                sectionArray = [self distinguishArrayWithArray:modelArray];
                
                //计算合计总价
                for (MZPriceModel *mzmodel in modelArray) {
                    allValue +=mzmodel.total_fee.floatValue;
                }
    
                //再次进行分类得到同一类相加价格相同数据显示一条,后的数据整理 
                SameArray = [self TwodistinguishArrayWithArray:sectionArray];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    [self setUI];
                });
                
    // 分类的数组
    -(NSMutableArray *)distinguishArrayWithArray:(NSArray *)array
    {
        
        
        //初始化一个空数组 用于return
        NSMutableArray * arrayAll = [[NSMutableArray alloc]init];
        
        //找到所有要对比的数据
        NSMutableArray * mutableArrayKey = [[NSMutableArray alloc]init];
        
        for (int i=0; i<array.count; i++)
        {
            
            MZPriceModel * model = [array objectAtIndex:i];
            
            if (model.accounting_type == nil&&[model.accounting_type isEqualToString:@""]) {
                model.accounting_type = @"0";
            }
            [mutableArrayKey addObject:model.accounting_type];
        }
        
        
        //过滤所有重复的数据
        NSArray * indexArray = [NSArray arrayWithArray:mutableArrayKey];
        NSSet * set = [NSSet setWithArray:indexArray];
        indexArray = [set allObjects];
        //排序
        indexArray = [indexArray sortedArrayUsingSelector:@selector(compare:)];
        
        
        
        for (int i=0; i<indexArray.count; i++)
        {
            //把每个不重复的数据当做一类数据
            NSMutableArray * muatbleArray = [[NSMutableArray alloc]init];
            
            //把原始数据分别归类
            for (int a=0; a<array.count; a++)
            {
                MZPriceModel * model = [array objectAtIndex:a];
                
                if ([model.accounting_type isEqualToString:[indexArray objectAtIndex:i]])
                {
                    [muatbleArray addObject:model];
                }
            }
            
            //获取所有归类过的数据
            [arrayAll addObject:muatbleArray];
        }
        return arrayAll;
    }

    //同类中整理数据

    -(NSArray *)TwodistinguishArrayWithArray:(NSArray *)arrayOld
    {
        NSMutableArray * arrarAll  = [[NSMutableArray alloc]init];
        //初始化一个空数组 用于return
        //把每个重复的数据当做一类数据
        
        for( int k=0;k<arrayOld.count;k++)
        {
            NSMutableArray * array2 =[NSMutableArray array];
            
            NSMutableArray * array = [NSMutableArray array];
            //同一类的
            array =arrayOld[k];
            
            //得到相同类目的数组
            for (int i=0; i<array.count-1; i++)
            {
                //  把原始数据分别归类
                for (int a=1+i; a<array.count; a++)
                {
                   
                    MZPriceModel * modeli = [array objectAtIndex:i];
                    MZPriceModel * modela = [array objectAtIndex:a];
                    
                    if (modeli.name==nil||[modeli.name isEqualToString:@""]) {
                  
                    }else{
                    if ([modela.name isEqualToString:modeli.name]  && [modela.price isEqualToString:modeli.price])
                    {
                        // NSArray *contentList = @[model.name,model.quantity,model.price,[NSString stringWithFormat:@"%0.2f",model.total_fee.floatValue]];
                        
                        NSString* quani = modeli.quantity;
                        NSString* quana = modela.quantity;
                        NSString *danjia = modela.price;
                        int zongliang =quana.intValue+quani.intValue;
                        
                        modeli.quantity =[NSString stringWithFormat:@"%d",zongliang];
                        modeli.total_fee =[NSString stringWithFormat:@"%0.1f",danjia.floatValue*zongliang];
                        
                        [array removeObjectAtIndex:a];
                        MZPriceModel * kong =[[MZPriceModel alloc]init];
                        [array insertObject:kong atIndex:a];
                 
                    }
                    
                    }
                }
                
            }
          
            for (MZPriceModel * mo in array) {
                
                if (mo.name) {
                    [array2 addObject:mo];
                }
                
                
            }
        
            [arrarAll addObject:array2];
         
        }
        return arrarAll;
    }
  • 相关阅读:
    深入浅出js单例模式
    前端常见面试-存储/缓存篇
    JavaScript内存管理
    delete操作符
    解决window.location.href跳转无效问题解决办法
    前端程序员经常忽视的一个JavaScript面试题
    【华为云技术分享】漫谈LiteOS-端云互通组件-MQTT开发指南(上)
    【华为云技术分享】序列特征的处理方法之二:基于卷积神经网络方法
    【华为云技术分享】原来CTR预估模型的发展有这样的规律
    【华为云技术分享】在家办公怎么弄?华为云DevCloud宝典一看就懂——项目管理篇
  • 原文地址:https://www.cnblogs.com/xujiahui/p/6934079.html
Copyright © 2011-2022 走看看