標籤:for .text json ring src 會議 values interface 初始化
在APP開發中經常碰到這種情況,後台給的資料需要我們按照某種條件分組。比如我的項目中:需要把數組中的資料按照時間分類分組,同一天的資料放在一起比如8-20號的資料分一組,8-21號的資料分一組。代碼如下:
#import "ViewController.h"#import "model.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{ NSMutableArray *userModelArr; NSMutableArray *tempArray; NSMutableArray *_dateArr; NSMutableArray *timeArr; NSArray * arrrrrrrrrr; }@property(nonatomic,strong)UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.title = @"會議"; _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; [self shuju]; }-(void)shuju{ tempArray = [NSMutableArray array]; NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://dev.brc.com.cn:8085/api/meeting/list?access_token=1b5e8731e5ff44928991b3098fe52464&type=32&showrows=10&page=1"]; // 通過URL初始化task,在block內部可以直接對返回的資料進行處理 NSURLSessionTask *task =[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) { NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]]; NSLog(@"999%@22",dic); id jsonArr = dic[@"data"]; NSMutableArray *allTimeArr = [NSMutableArray array]; for (NSDictionary *ordersDic in jsonArr) { //1.取出所有出現得時間 [allTimeArr addObject:[ordersDic[@"starttime"] substringToIndex:10]]; } arrrrrrrrrr = [self arrayWithMemberIsOnly:allTimeArr]; NSLog(@"%@",arrrrrrrrrr); for (NSString *nowTim in arrrrrrrrrr) { NSMutableArray *arr = [[NSMutableArray alloc] init]; for (NSDictionary *ordersDicTwo in jsonArr) { NSString *twoTim = [ordersDicTwo[@"starttime"] substringToIndex:10]; if([twoTim isEqualToString:nowTim]){ //2.將每個字典儲存在模型數組中 model *tianjianModel = [[model alloc]init]; [tianjianModel setValuesForKeysWithDictionary:ordersDicTwo]; [arr addObject:tianjianModel]; } } [tempArray addObject:arr]; } [_tableView reloadData]; }]; [task resume];// });}// 告訴TableView對應的Section要顯示幾行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSArray *arr = tempArray[section]; return arr.count;}// 告訴TableView要顯示什麼內容,當Cell顯示的時候就會調用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } NSArray *arr = tempArray[indexPath.section]; model *cellModel = arr[indexPath.row]; cell.textLabel.text = cellModel.title; return cell;}// 返回分組(Section)數 Section- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return tempArray.count;}// 返回組頭部的Title- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"第%ld組", section];}// 返回組頭部(段頭)的高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 30;}//去除數組中重複的-(NSArray *)arrayWithMemberIsOnly:(NSArray *)array{ NSMutableArray *categoryArray =[[NSMutableArray alloc] init]; for (unsigned i = 0; i < [array count]; i++) { @autoreleasepool { if ([categoryArray containsObject:[array objectAtIndex:i]]==NO) { [categoryArray addObject:[array objectAtIndex:i]]; } } } return categoryArray;}
iOS開發 伺服器請求出來的資料按日期重新分組