parsing - group array of dictionaries by date from xml in iOS -
here code. please help.
nsdictionary *xmldict = [nsdictionary dictionarywithxmlstring:str]; nsarray *fixtureslist1 = [[xmldict objectforkey:@"matches"] objectforkey:@"ematches"]; nsmutabledictionary *dictionarybydate = [nsmutabledictionary new]; for(nsdictionary *dictionary in fixtureslist1) { nsstring *datestring = dictionary[@"matchdate"]; nsmutablearray *arraywithsamedate = self.resultssection[datestring]; if(! arraywithsamedate) { arraywithsamedate = [nsmutablearray new]; self.resultssection[datestring] = arraywithsamedate; } [arraywithsamedate addobject: dictionary]; } //nslog(@"dictionarybydate:%@",self.resultssection); nssortdescriptor *descriptor=[[nssortdescriptor alloc] initwithkey:@"self" ascending:no]; nsarray *descriptors=[nsarray arraywithobject: descriptor]; reverseorder=[[self.resultssection allkeys] sortedarrayusingdescriptors:descriptors]; nslog(@"results:%@",reverseorder);
@property (nonatomic, retain) nsmutabledictionary *sections;
in viewdidload
self.sections = [[nsmutabledictionary alloc]init]; [self setupsections]; - (void)setupsections { bool found; // loop through items , create our keys (yourobject *item in self.items) { nsstring *date= item.matchdate; found = no; (nsstring *str in [self.sections allkeys]) { if ([str isequaltostring:date]) { found = yes; } } if (!found) { [self.sections setobject:[[nsmutablearray alloc] init] forkey:date]; } } // loop again , sort items respective keys (yourobject *item in self.items) { [[self.sections objectforkey:item.matchdate addobject:item]; } (nsstring *key in [self.sections allkeys]) { [[self.sections objectforkey:key] sortusingdescriptors:[nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@“matchdate" ascending:no]]]; } //reload tableview using self.sections [self.tableview reloaddata]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [[self.sections allkeys] count]; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { nsstring *sectionheader = @""; if(!isfiltered) { sectionheader = [[[self.sections allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)] objectatindex:section]; } return sectionheader; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { int rowcount; rowcount = [[self.sections objectforkey:[[[self.sections allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)] objectatindex:section]] count]; return rowcount; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell; cell = (uitableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; if (cell == nil) { cell = [[uitableviewcellalloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } yourobject *item = [[self.sections objectforkey:[[[self.sections allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)] objectatindex:indexpath.section]] objectatindex:indexpath.row]; return cell; }
Comments
Post a Comment