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;
}
原文地址:https://www.cnblogs.com/xujiahui/p/6934079.html