Data requested by the iOS development server is re-grouped by date

Source: Internet
Author: User

This is often the case in app development, where data from the backend needs to be grouped according to certain criteria. For example, in my project: The data in the array needs to be grouped by time, data on the same day, such as the data of number 8-20, is divided into groups, and the data of number 8-21 is divided into groups. The code is as follows:

#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 = @ "Meeting";    _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 "];   Initializes a task through a URL, which can be processed directly within the block with the returned data Nsurlsessiontask *task =[session datataskwithurl:url completionhandler:^ (NSData *data, NSURLResponse *response, nserror* error) {nsdictionary *dic = [Nsdictionary dictionarywithdictionary:[nsjsonserialization JSONObjec                Twithdata:data options:kniloptions Error:nil]];                NSLog (@ "999%@22", DIC);                ID Jsonarr = dic[@ "Data"];        Nsmutablearray *alltimearr = [Nsmutablearray array]; For (Nsdictionary *ordersdic in Jsonarr) {//1. Remove all occurrences of time [Alltimearr ADDOBJECT:[ORDERSDI        c[@ "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[@] Star       Ttime "] substringtoindex:10];         if ([Twotim Isequaltostring:nowtim]) {//2. Save each dictionary in the model array *tianjianmodel                    = [[Model Alloc]init];                    [Tianjianmodel Setvaluesforkeyswithdictionary:ordersdictwo];                                    [Arr Addobject:tianjianmodel];        }} [Temparray Addobject:arr];       } [_tableview Reloaddata];        }]; [Task resume];//});} Tell tableView the corresponding section to display a few lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger)    section{Nsarray *arr = temparray[section]; return arr.count;} Tell TableView what to display, and when the cell is displayed, it will be called-(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;} Returns the grouping (section) number section-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return temparray.count;} Returns the title-(NSString *) of the head of the group TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{return [ NSString stringwithformat:@ "%ld Group", section];} Returns the height of the group head (segment header)-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{return 30;} Remove duplicate in array-(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;} 

Data requested by the iOS development server is re-grouped by date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.