ios - Adding an NSManagedObject to an NSMutableArray via didSelectRowAtIndexPath -
i have core data project set up, i've got nsfetchedresultscontroller
working properly, nspredicates
works properly, , i'm onto doing useful data displayed on screen.
right now, have uitableviewcellaccessory working toggle checked/unchecked cells, i'm returning nil when try add selectedmanagedobject
myarrayofmanagedobjects
via didselectrowatindexpath
.
what proper way add/remove nsmanagedobject
nsmutablearray
via didselectrowatindexpath
?
here relevant properties:
@property (nonatomic, strong) nsmanagedobject *selectedmanagedobject; @property (nonatomic, strong) nsmutablearray *myarrayofmanagedobjects;
here's viewdidload
array want dump `nsmanagedobject's instantiated:
- (void)viewdidload { [super viewdidload]; // standard core data setup stuff // instantiate list store myarrayofmanagedobjects self.myarrayofmanagedobjects = [[nsmutablearray alloc]init]; }
here's didselectrowatindexpath
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsmanagedobject *selectedmanagedobject = self.selectedmanagedobject; // set checkmark accessory selected row. // todo: add object array if ([tableview cellforrowatindexpath:indexpath].accessorytype == uitableviewcellaccessorynone) { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorycheckmark]; // ** problem lives here [self.myarrayofmanagedobjects insertobject:self.selectedmanagedobject atindex:indexpath.row]; nslog(@"added %@ myarrayofmanagedobjectsarray", selectedmanagedobject.myobjectdescription); nslog(@"myarrayofmanagedobjects contains %@", self.myarrayofmanagedobjects); [tableview deselectrowatindexpath:indexpath animated:yes]; } else { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorynone]; // ** problem lives here [self.myarrayofmanagedobjects removeobjectatindex:indexpath.row]; nslog(@"removed %@ myarrayofmanagedobjectsarray", selectedmanagedobject.myobjectdescription); nslog(@"myarrayofmanagedobjects contains %@", self.myarrayofmanagedobjects); [tableview deselectrowatindexpath:indexpath animated:yes]; } }
update: problem solved
in case wasn't clear, stuff on tableview
fetchedresultscontroller
. task add items separate nsmutablearray
i'm going save. here's got working:
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // ** problem solved: needed tell compiler selectedmanagedobject self.selectedmanagedobject = [self.fetchedresultscontroller objectatindexpath:indexpath]; // set checkmark accessory selected row. if ([tableview cellforrowatindexpath:indexpath].accessorytype == uitableviewcellaccessorynone) { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorycheckmark]; [self.myarrayofmanagedobjects addobject:self.selectedmanagedobject]; nslog(@"added **%@** myarrayofmanagedobjects", self.selectedmanagedobject.description); nslog(@"myarrayofmanagedobjects contains %lu objects", (unsigned long)[self.myarrayofmanagedobjects count]); [tableview deselectrowatindexpath:indexpath animated:yes]; } else { [[tableview cellforrowatindexpath:indexpath] setaccessorytype:uitableviewcellaccessorynone]; [self.myarrayofmanagedobjects removeobject:self.selectedmanagedobject]; nslog(@"removed **%@** mymanagedobjectarray", self.selectedmanagedobject.description); nslog(@"myarrayofmanagedobjects contains %lu objects", (unsigned long)[self.myarrayofmanagedobjects count]); [tableview deselectrowatindexpath:indexpath animated:yes]; } }
i think there wrong logic put in place. or maybe don't understand it.
you have array containing objects displayed, right? self.myarrayofmanagedobjects
. assume you're filling somewhere else, otherwise wouldn't asking question.
then select , :
nsmanagedobject *selectedmanagedobject = self.selectedmanagedobject;
means (as know) newly instantiated nsmanagedobject
same 1 .h, is, can see, not instantiated.
so you're pretty playing nil
every line following one.
what should :
nsmanagedobject *selectedmanagedobject = [self.myarrayofmanagedobjects objectatindex:indexpath.row];
or
self.selectedmanagedobject = [self.myarrayofmanagedobjects objectatindex:indexpath.row];
now have reference right object in array of objects , can start manipulating it.
from can see add or remove object array, add or remove reference new array.
[myarray addobject:object]; [myarray removeobject:object];
since they're references, they'll find it.
if you're modifying data of same array built tableview, have call
[self.tableview reloaddata];
to see newly updated tableview
Comments
Post a Comment