最近在寫公司的項目的時候,遇到一個需求,后臺返回一些交易列表,前端 app 需要根據(jù)后臺返回的數(shù)據(jù) Model 里面的時間對數(shù)據(jù)分組排序,大概是這樣
具體需求是本年只顯示月份,當(dāng)月顯示本月,不是本年的數(shù)據(jù)連帶顯示年份.自己寫了兩個方法,遇到了很多 crash 和數(shù)據(jù)重復(fù)的問題,汗....,完成之后記錄一下得失.感謝金陽和新林同學(xué)的大力支持.
Simulator Screen Shot - iPhone X - 2017-11-03 at 17.12.52.png
第一 - 后臺返回的數(shù)據(jù)樣式,如果你們后臺返回的不是字符串而是時間戳之類的,需要進行轉(zhuǎn)換,這個就不提了,問度娘就知道了
數(shù)據(jù)格式.png
一共有五個參數(shù),需要對時間進行處理.先思考,在寫代碼,思考完成之后,代碼是很快滴.
第二 - 處理數(shù)據(jù)
- 一般來說,我們都會創(chuàng)建一個 Model 對象去接收服務(wù)器的數(shù)據(jù),我們也是一樣,當(dāng)請求完成以后,如果數(shù)據(jù)不為空,進入數(shù)據(jù)處理,self.dataArray是一個數(shù)組用來存儲字典的.數(shù)組一定要記得初始化.
#define VALIDARRAY(array) [array isKindOfClass:[NSArray class]]&&array!=nil
- 判斷是否為空,不為空處理數(shù)據(jù),這是自己寫的宏,當(dāng)然啦也不一定是數(shù)組,我們自己在回調(diào)里處理成了數(shù)組
if (weakSelf.page == 1)
{
[weakSelf.dataArray removeAllObjects];
}
if (VALIDARRAY(result))
{
//處理數(shù)據(jù)脚线,根據(jù)日期進行分組
[weakSelf handleBillData:result];
//刷新列表
[weakSelf.billsTableView reloadData];
}
- 調(diào)用大招方法,首先我們需要獲取當(dāng)前的時間來對比,寫在 viewDidLoad
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
currentYear =components.year;//成員變量
currentMonth =components.month;//成員變量
- 因為 OC 語言沒有二維數(shù)組,我們就用字典來實現(xiàn)
- 我們的思路是把數(shù)據(jù)轉(zhuǎn)換成一個字典, key 用來區(qū)分不同時間的數(shù)據(jù),例如2016年的 key 是2016,的字典2017年的數(shù)據(jù)時間的 key 是2017的字典,字典對應(yīng)的 value 用來存儲相同時間的 Model數(shù)組.
- 處理服務(wù)器時間,NSDateComponents是用來截取時間里的年-月-日-時-分-秒,我們只需要年月.
-(void) handleBillData:(NSArray *)data
{
for (WBBillsModel *model in data)//WBBillsModel是自定義的 Model
{
NSString *key;
NSDate *date=[self.dateFormatter dateFromString:model.tradeDateStr];// 把model 里面的時間轉(zhuǎn)換成date 類型
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
NSInteger billYear=[components year];
NSInteger billMonth=[components month];
if (billYear ==currentYear)//本年,對比之后的結(jié)果是本年
{
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 里面 的小數(shù)組,小數(shù)組存儲行里面的 Model,
//如果不包含,加進 dataarray 添加一個新 Key 的字典.這是用來判斷組的字典.
{
isContained =YES;
containedIndex =i;
break;
}
}
if (isContained)
{//如果包含,加進 dic 里面 的小數(shù)組,小數(shù)組存儲行里面的 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];
}
}
}
第三 - 顯示數(shù)據(jù),現(xiàn)在我們的dataarray 里面有了很多條數(shù)據(jù)
dataArray.png
- 在表格里面調(diào)用
#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;
}