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

本文介绍了一种iOS应用中根据后台返回的交易数据,按时间进行分组排序的方法。通过将数据转换为字典并利用NSDateComponents进行年月的截取,实现了对交易数据的有效组织。

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

具体需求是本年只显示月份,当月显示本月,不是本年的数据连带显示年份.自己写了两个方法,遇到了很多 crash 和数据重复的问题,汗....,完成之后记录一下得失.感谢金阳和新林同学的大力支持. ####第一 - 后台返回的数据样式,如果你们后台返回的不是字符串而是时间戳之类的,需要进行转换,这个就不提了,问度娘就知道了

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

####第二 - 处理数据

  • 一般来说,我们都会创建一个 Model 对象去接收服务器的数据,我们也是一样,当请求完成以后,如果数据不为空,进入数据处理,self.dataArray是一个数组用来存储字典的.数组一定要记得初始化.
#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];
        }

复制代码
  • 调用大招方法,首先我们需要获取当前的时间来对比,写在 viewDidLoad
 NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    currentYear =components.year;//成员变量
    currentMonth =components.month;//成员变量
复制代码
  • 因为 OC 语言没有二维数组,我们就用字典来实现
  • 我们的思路是把数据转换成一个字典, key 用来区分不同时间的数据,例如2016年的 key 是2016,的字典2017年的数据时间的 key 是2017的字典,字典对应的 value 用来存储相同时间的 Model数组.
  • 处理服务器时间,NSDateComponents是用来截取时间里的年-月-日-时-分-秒,我们只需要年月.
-(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 里面有了很多条数据

  • 在表格里面调用
#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];
}
复制代码
  • 有多少组 section
-(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;
}
复制代码

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

转载于:https://juejin.im/post/5a420847f265da432d285b75

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值