ios - Show sections with core data -
i want code todo app , display 2 sections. 1 entrys, have done , 1 done things. want that:
things not done yet todo 1 todo 2 things done todo 3 todo 4
at moment have 1 section undone todos. code table view @ moment looks this
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { nsarray *array = [[nsarray alloc] initwithobjects:@"to do",@"done",nil]; return [array objectatindex:section]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [[self.fetchedresultscontroller sections] count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [self.fetchedresultscontroller sections][section]; return [sectioninfo numberofobjects]; }
and code fetchedresultscontroller looks this:
- (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsmanagedobjectcontext* context = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; // edit entity name appropriate. nsentitydescription* entity = [nsentitydescription entityforname:@"todo" inmanagedobjectcontext:context]; [fetchrequest setentity:entity]; // set batch size suitable number. [fetchrequest setfetchbatchsize:20]; // edit sort key appropriate. nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"timestamp" ascending:yes]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors]; nspredicate *predicate = [nspredicate predicatewithformat:@"done == %@", @(no)]; [fetchrequest setpredicate:predicate]; // edit section name key path , cache name if appropriate. // nil section name key path means "no sections". nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:@"done" cachename:nil]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller; nserror *error = nil; if (![self.fetchedresultscontroller performfetch:&error]) { // replace implementation code handle error appropriately. // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } return _fetchedresultscontroller; }
objects key "no" not done , key "yes" done. don't know how display both values in different sections.
edit
i found solution problem…
nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:@"done" cachename:nil]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller;
that solved problem. undone item shows @ done. or if there 2 done items appear under "todo"
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { nsarray *array = [[nsarray alloc] initwithobjects:@"todo",@"done",nil]; return [array objectatindex:section]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [[self.fetchedresultscontroller sections] count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [self.fetchedresultscontroller sections][section]; return [sectioninfo numberofobjects]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; [self configurecell:cell atindexpath:indexpath]; return cell; }
thats actual code that… looks that. undone todo unchecked, done todo checked. http://img.xnmn.de/i/2353ad.png if check undone todo done, both appear @ "todo" http://img.xnmn.de/i/5edff4.png
if code right created section fine.
- (uitableviewcell *)tableview:(uitableview *)intableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *identifier = @"entry"; uitableviewcell *cell = [intableview dequeuereusablecellwithidentifier:identifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:identifier]; } yourobject *cellentry = [_fetchedresultscontroller objectatindexpath:indexpath]; //init cell }
this should choose right entry right section otherwise line set section wrong.
the index path chooses right section.
Comments
Post a Comment