iOS開發 伺服器請求出來的資料按日期重新分組

來源:互聯網
上載者:User

標籤: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開發 伺服器請求出來的資料按日期重新分組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.