iphone - How Can I Get String Dates from Coredata in Asending or Descending Order in IOS -
in applicaiton inserting dates in coredata strings.how can dates in asending or descending order.i using below code fetching date strings coredata , displaying results in section table , fetched dates section titles .
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. } -(ibaction)insert:(id)sender { nsmanagedobjectcontext *moc=[[self appdelegate] managedobjectcontext]; stringdates *entity=[nsentitydescription insertnewobjectforentityforname:@"stringdates" inmanagedobjectcontext:moc]; entity.unused=inserttextfield.text; entity.message=@"123456789"; nserror *error; [moc save:&error]; [inserttextfield resignfirstresponder]; } -(nsfetchedresultscontroller *)fetchedresultscontroller { nsmanagedobjectcontext *moc = [[self appdelegate] managedobjectcontext]; if (moc == nil) return nil; nsentitydescription *entity = [nsentitydescription entityforname:@"stringdates" inmanagedobjectcontext:moc]; nssortdescriptor *sd1 = [[nssortdescriptor alloc] initwithkey:@"unused" ascending:yes]; nsarray *sortdescriptors = [nsarray arraywithobjects:sd1,nil]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; [fetchrequest setentity:entity]; [fetchrequest setsortdescriptors:sortdescriptors]; fetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:moc sectionnamekeypath:@"unused" cachename:nil]; [fetchedresultscontroller setdelegate:self]; nserror *error = nil; if (![fetchedresultscontroller performfetch:&error]) { nslog(@"error performing fetch: %@", error); } return fetchedresultscontroller; } - (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [stringdatestableview reloaddata]; } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [[[self fetchedresultscontroller] sections] count]; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nsarray *sections = [[self fetchedresultscontroller] sections]; if (section < [sections count]) { id <nsfetchedresultssectioninfo> sectioninfo = [sections objectatindex:section]; return sectioninfo.numberofobjects; } return 0; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { //static nsstring *cellidenitifier=@"cell"; stringdates *userrecentchat=[[self fetchedresultscontroller] objectatindexpath:indexpath]; uitableviewcell *cell=[tableview dequeuereusablecellwithidentifier:nil]; if(cell==nil) { cell=[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:nil]; } cell.detailtextlabel.text=userrecentchat.message; return cell; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { nsarray *sections = [[self fetchedresultscontroller] sections]; if (section < [sections count]) { id <nsfetchedresultssectioninfo> sectioninfo = [sections objectatindex:section]; return sectioninfo.name; } return @""; } but dates strings not coming either asending order or descendibg order.can solve problem.thanks inadvance.
you use nsdateformatter object create nsdate objects , store coredata. sort them ascending/descending using nssortdescriptor.
just take quick nsdateformatter reference https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/classes/nsdateformatter_class/reference/reference.html
and dateformattingguide: https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/dataformatting/dataformatting.html#//apple_ref/doc/uid/10000029i
it's super easy do.
Comments
Post a Comment