?前端數據無分組返回,自遍歷設置數據按時間分組顯示
直接擼代碼
拿到加載后的總數據后,通過下面兩個方法來定位數據位置
//獲得每個時間相同的數量
- (NSArray *)getRowNumberArray {
? ? @autoreleasepool {
? ? ? ? NSMutableArray *timeArr=[NSMutableArray array];
? ? ? ? for(inti=0; i
? ? ? ? ? ? @autoreleasepool {
? ? ? ? ? ? ? ? SystemNotification*model=self.dataArray[i];
? ? ? ? ? ? ? ? [timeArr addObject:model.create_at];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? NSInteger num=1;
? ? ? ? NSMutableArray *arr2=[NSMutableArray array];//記錄每個時間相同的數量
? ? ? ? for(inti=0; i
? ? ? ? ? ? @autoreleasepool {
? ? ? ? ? ? ? ? if([timeArr[i] isEqualToString:timeArr[i+1]]){
? ? ? ? ? ? ? ? ? ? num++;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? [arr2 addObject:[NSNumber numberWithDouble:num]];
? ? ? ? ? ? ? ? ? ? num=1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? [arr2 addObject:[NSNumber numberWithDouble:num]];
? ? ? ? returnarr2;
? ? }
}
//獲得每個不同時間第一個數的位置
- (NSArray *)getIndexArray {
? ? @autoreleasepool {
? ? ? ? NSArray *arr=[self getRowNumberArray];
? ? ? ? NSMutableArray *arr1=[NSMutableArray array];
? ? ? ? [arr1 addObject:@0];
? ? ? ? for(inti=0; i
? ? ? ? ? ? @autoreleasepool {
? ? ? ? ? ? ? ? NSInteger index=[[arr1 lastObject] integerValue]+[arr[i] integerValue];
? ? ? ? ? ? ? ? [arr1 addObject:[NSNumber numberWithDouble:index]];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? returnarr1;
? ? }
}
以下tablecell中的使用方法:
- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {
? ? NSArray *arr=[self getIndexArray];
? ? NSInteger index=[arr[indexPath.section] integerValue];
? ? SystemNotification*model=self.dataArray[index+indexPath.row];// 拿到模型
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? NSMutableSet *set=[NSMutableSet set];// 不重復元素來確定section數量
? ? for(inti=0; i< self.dataArr.count;i++) {
? ? ? ? SystemNotification*model=self.dataArray[i];
? ? ? ? [set addObject:model.create_at];
? ? }
? ? return set.count;
}
- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section {
? ? NSArray *arr=[self getRowNumberArray];
? ? return [arr[section] integerValue];// section中的cell數量
}
OK啦,少年!