ios - UITableView updates for correct number of managedObjects, but won't display values -
i have tableviewcontroller
displays "add" (static) cell @ top , want list attributes of objects (dynamic cells) pulled managedobjectcontext
. found this post helpful in getting "add" cell working, i've saved objects to managedobjectcontext
, i've discovered doesn't display attributes of objects in managedobjectcontext
.
to "see" what's going on, made "dynamic" cells orange. when add categories managedobjectcontext
, # of orange cells updates properly, can't attribute of managedobject
(an nsstring
) display in cell.
i threw breakpoint in after fetchrequest
completed see if there locationcategories
(my nsmanagedobject
) in array--there are.
categorytvc.h
@property (nonatomic, strong) nsmanagedobjectcontext *managedobjectcontext; @property (nonatomic, strong) locationcategory *category;
categorytvc.m
#define number_of_static_cells 1 // can updated // sets array dump locationcategories @property (nonatomic, strong) nsarray *locationcategories; // cell identifier strings static nsstring *dynamicidentifier = @"dynamicidentifier"; static nsstring *staticidentifier = @"staticidentifier"; - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; self.title = @"select category"; // core data appdelegate *appdelegate = (appdelegate *)[[uiapplication sharedapplication]delegate]; self.managedobjectcontext = [appdelegate managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; [fetchrequest setentity:[nsentitydescription entityforname:@"locationcategory" inmanagedobjectcontext:self.managedobjectcontext]]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"categoryname" ascending:yes]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:&sortdescriptor count:1]; [fetchrequest setsortdescriptors:sortdescriptors]; nsarray *categories = [self.managedobjectcontext executefetchrequest:fetchrequest error:nil]; self.locationcategories = categories; // duplicate line above, modeling apple's sample code } - (void)viewdidload { [super viewdidload]; [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:dynamicidentifier]; [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:staticidentifier]; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { if (section == 0) { return @""; } else if (section == 1) { return @"categories"; } else { // shut compiler return nil; } } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section == 0) { return 1; } else { return self.locationcategories.count; } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0) { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:staticidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:staticidentifier]; } cell.textlabel.text = @"create new category"; return cell; } else if (indexpath.section == 1) { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:dynamicidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:dynamicidentifier]; } nsmanagedobject *locationcategory = [self.locationcategories objectatindex:indexpath.row]; cell.textlabel.text = [locationcategory valueforkey:@"categoryname"]; cell.backgroundcolor = [uicolor orangecolor]; // todo: gives cell color see how many self.locationcategories there return cell; } return nil; }
just sake of completeness, i'm adding locationcategory class below:
**locationcategory.h** #import <foundation/foundation.h> #import <coredata/coredata.h> @class pointofinterest; @interface locationcategory : nsmanagedobject @property (nonatomic, retain) id categorycolor; @property (nonatomic, retain) nsstring * categoryname; @property (nonatomic, retain) nsset *pointofinterest; @end **locationcategory.m** #import "locationcategory.h" #import "pointofinterest.h" @implementation locationcategory @dynamic categorycolor; @dynamic categoryname; @dynamic pointofinterest; @end
you need call reloaddata on table view after fetch request (so last line in viewwillappear).
also, when register class cells, there's no need if (cell==nil) clause because cell never nil.
Comments
Post a Comment