代码质量程序员

iOS -中表格按时间戳分组排序

2017-11-03  本文已影响451人  五蕴盛

最近在写公司的项目的时候,遇到一个需求,后台返回一些交易列表,前端 app 需要根据后台返回的数据 Model 里面的时间对数据分组排序,大概是这样

具体需求是本年只显示月份,当月显示本月,不是本年的数据连带显示年份.自己写了两个方法,遇到了很多 crash 和数据重复的问题,汗....,完成之后记录一下得失.感谢金阳和新林同学的大力支持.


Simulator Screen Shot - iPhone X - 2017-11-03 at 17.12.52.png

第一 - 后台返回的数据样式,如果你们后台返回的不是字符串而是时间戳之类的,需要进行转换,这个就不提了,问度娘就知道了

数据格式.png

一共有五个参数,需要对时间进行处理.先思考,在写代码,思考完成之后,代码是很快滴.

第二 - 处理数据

#define VALIDARRAY(array)   [array isKindOfClass:[NSArray class]]&&array!=nil
 if (weakSelf.page == 1)
        {
            [weakSelf.dataArray removeAllObjects];
        }
        if (VALIDARRAY(result))
        {
            //处理数据,根据日期进行分组
            [weakSelf handleBillData:result];
            //刷新列表
            [weakSelf.billsTableView reloadData];
        }

 NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    currentYear =components.year;//成员变量
    currentMonth =components.month;//成员变量
-(void) handleBillData:(NSArray *)data
{
    for (WBBillsModel *model in data)//WBBillsModel是自定义的 Model
    {
     
        NSString *key;

        NSDate *date=[self.dateFormatter dateFromString:model.tradeDateStr];// 把model 里面的时间转换成date 类型
   
        NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
        
        NSInteger billYear=[components year];
        NSInteger billMonth=[components month];
        
        if (billYear ==currentYear)//本年,对比之后的结果是本年
        {
            if (billMonth ==currentMonth)//本月
            {
                key =@"本月";
            }
            else//其他月
            {
                key =[NSString stringWithFormat:@"%ld月",billMonth];
            }
        }
        else//非本年
        {
            key =[NSString stringWithFormat:@"%ld年%ld月",billYear,billMonth];
        }
        
        BOOL isContained =NO;//dataArray 是否包含key
        NSInteger containedIndex = 0;//记录index
        
        for (int i=0;i<self.dataArray.count;i++)
        {
            NSMutableDictionary *dic =[self.dataArray AX_objectAtIndex:i];
            if ([[dic.allKeys AX_objectAtIndex:0] isEqualToString:key])//判断是否包含已存有的 Key, 如果包含,加进 dic 里面 的小数组,小数组存储行里面的 Model,如果不包含,加进 dataarray 添加一个新 Key 的字典.这是用来判断组的字典.
            {
                isContained =YES;
                containedIndex =i;
                break;
            }
        }
            
            if (isContained)//如果包含,加进 dic 里面 的小数组,小数组存储行里面的 Model
            {
                NSMutableDictionary *dic =[self.dataArray AX_objectAtIndex:containedIndex];
                
                NSMutableArray *array =dic[key];
                [array AX_addObject:model];
                [dic AX_setObject:array forKey:key];
            }
            else //如果不包含,加进 dataarray 添加一个新 Key 的字典.这是用来判断组的字典.
            {
                NSMutableDictionary *dic =[NSMutableDictionary dictionary];
                NSMutableArray *array =[NSMutableArray new];
                [array addObject:model];
                [dic AX_setObject:array forKey:key];
                
                [self.dataArray AX_addObject:dic];//最终有多少组字典,我们就给我们的表格分几个组.
            }
    }
}

第三 - 显示数据,现在我们的dataarray 里面有了很多条数据

dataArray.png
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * billsCellID = @"billsCellID";
    WBBillsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:billsCellID];
    
    if (!cell)
    {
        cell = [[WBBillsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:billsCellID];
    }
    
    cell.model =[self modelForIndexPath:indexPath];
    
    return cell;
}
-(WBBillsModel *)modelForIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section =indexPath.section;
    NSInteger row=indexPath.row;
    
    NSMutableDictionary *dic =[self.dataArray AX_objectAtIndex:section];//这个是自己写的防止为空的方法,可以用自己的
    NSMutableArray *array =dic[[dic.allKeys AX_objectAtIndex:0]];
    return (WBBillsModel *)[array AX_objectAtIndex:row];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (VALIDDICTIONARY([self.dataArray AX_objectAtIndex:section]))
    {
        NSDictionary *sectionBillDic =[self.dataArray AX_objectAtIndex:section];
        NSArray *sectionBillArray =[sectionBillDic objectForKey:[sectionBillDic.allKeys AX_objectAtIndex:0]];
        
        if (VALIDARRAY(sectionBillArray))
        {
            return sectionBillArray.count;
        }
    }
    
    return 0;
}

好了,就这样,希望可以帮到你们,不要打赏,请给我点个赞吧!

上一篇 下一篇

猜你喜欢

热点阅读