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

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

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


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

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

数据格式.png

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

第二 - 处理数据

  • 一般来说,我们都会创建一个 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 里面有了很多条数据

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;
    //AX_objectAtIndex 这个是自己写的防止为空的方法,可以用自己的
    NSMutableDictionary *dic =[self.dataArray :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;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容